开发者

How to apply Style programmatically in CustomControl

开发者 https://www.devze.com 2023-03-13 13:07 出处:网络
I have a custom button,ColorPickerButton, in WPF and style \"ColorPickerButtonStyle\" is applied for it, which I have to apply in xmal like this;

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");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号