簡體   English   中英

如何在CustomControl中以編程方式應用Style

[英]How to apply Style programmatically in CustomControl

我有一個自定義按鈕,ColorPickerButton,在WPF和樣式“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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM