How to apply Style programmatically in CustomControl
I have a custom button,ColorPickerButton, in WPF and style "ColorPickerButtonStyle" is applied for it, which I have to apply in xmal like this;
<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" />
this working fine. But if I forget to apply style "{DynamicResource ColorPickerButtonStyle}" then button will look like death fish. What I am trying to do here is that I would like to apply this part <ResourceDictionary Source="RDColorPicker.xaml"/> and Style="{DynamicResource ColorPickerButtonStyle}" to be embedded in the implementation part of ColorP开发者_StackOverflow中文版ickerButton class like in the Constructor of Class;
public class ColorPickerButton:Button
{
....
public ColorPickerButton()
{
....
//How to call resourcedictionary and apply style "ColorPickerButtonStyle" for this button
}
}
Something like:
this.SetResourceReference(ColorPickerButton.StyleProperty, "ColorPickerButtonStyle");
精彩评论