3
我在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
}
}
這對我很有幫助。如果你給我一些提示和技巧。
感謝你
感謝@Zahid, 我必須加載的ResourceDictionary之前設置樣式。如: ResourceDictionary rd = new ResourceDictionary(); rd.Source = new Uri(「/ CustomWPFColorPicker; component/RDColorPicker.xaml」,System.UriKind.Relative); this.Resources.MergedDictionaries.Add(rd); this.SetResourceReference(ColorPickerButton.StyleProperty,「ColorPickerButtonStyle」); // This this workfine: //this.Style =(Style)this.FindResource(「ColorPickerButtonStyle」); – 2011-06-16 05:36:31
很高興知道:) – 2011-06-16 05:46:13