我正在制作一些封装类,它们封装了基类控件的设置细节,但我希望它们使用基类的样式。我可以使用以下 xaml 来实现这一点:
<Style TargetType="{x:Type bc:DerviedClass}" BasedOn="{StaticResource {x:Type etk:BaseClass}}"/>
我宁愿不使用 xaml,这样客户端程序员就不必将此行添加到他的 xaml 代码中。有没有办法在 C# 中做到这一点?
谢谢
我正在制作一些封装类,它们封装了基类控件的设置细节,但我希望它们使用基类的样式。我可以使用以下 xaml 来实现这一点:
<Style TargetType="{x:Type bc:DerviedClass}" BasedOn="{StaticResource {x:Type etk:BaseClass}}"/>
我宁愿不使用 xaml,这样客户端程序员就不必将此行添加到他的 xaml 代码中。有没有办法在 C# 中做到这一点?
谢谢
我找到了解决方案:
Style s = (Style)derivedObj.FindResource(baseType);
if(s != null)
{
Style derivedStyle = new Style(derivedObj.GetType(), s);
derivedObj.Style = derivedStyle;
}
您应该考虑使用 Themes/generic.xaml 文件设置自定义控件的样式/主题。
每当有人使用它时,此主题将成为默认主题。