如何在CustomControl中以编程方式应用样式



我在WPF中有一个自定义按钮ColorPickerButton,样式为"ColorPickerButtonStyle",我必须像这样在xmal中应用它;

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="RD_ColorThemes.xaml"/>
           <ResourceDictionary Source="RDColorPicker.xaml"/>
        </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary>
</UserControl.Resources>
<Grid x:Name="grd">
<cp:ColorPickerButton x:Name="btn" Width="25" Height="25" 
Style="{DynamicResource ColorPickerButtonStyle}" 
Click="ColorPickerButton_Click" />

这很好用。但若我忘记应用样式"{DynamicResource ColorPickerButtonStyle}",那个么按钮将看起来像死鱼。我在这里试图做的是,我想应用这一部分<ResourceDictionary Source="RDColorPicker.xaml"/>和Style="{DynamicResource ColorPickerButtonStyle}"嵌入到ColorPickerButton类的实现部分中,类似于类的构造函数中;

public class ColorPickerButton:Button
{
   ....
   public ColorPickerButton()
   {
      ....
      //How to call resourcedictionary and apply style "ColorPickerButtonStyle" for this      button 
   }
}

类似于:

this.SetResourceReference(ColorPickerButton.StyleProperty, "ColorPickerButtonStyle");

相关内容

  • 没有找到相关文章

最新更新