我在 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}”,那么按钮将看起来像死鱼。
我想在这里做的是,我想将这部分
public class ColorPickerButton:Button
{
....
public ColorPickerButton()
{
....
//How to call resourcedictionary and apply style "ColorPickerButtonStyle" for this button
}
}
最佳答案
类似于:
this.SetResourceReference(ColorPickerButton.StyleProperty, "ColorPickerButtonStyle");
关于c# - 如何在 CustomControl 中以编程方式应用样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6366851/