0

我正在制作一些封装类,它们封装了基类控件的设置细节,但我希望它们使用基类的样式。我可以使用以下 xaml 来实现这一点:

<Style TargetType="{x:Type bc:DerviedClass}" BasedOn="{StaticResource {x:Type etk:BaseClass}}"/>

我宁愿不使用 xaml,这样客户端程序员就不必将此行添加到他的 xaml 代码中。有没有办法在 C# 中做到这一点?

谢谢

4

2 回答 2

0

我找到了解决方案:

Style s = (Style)derivedObj.FindResource(baseType);

if(s != null)
{
    Style derivedStyle = new Style(derivedObj.GetType(), s);
    derivedObj.Style = derivedStyle;
}
于 2012-02-16T02:03:43.533 回答
0

您应该考虑使用 Themes/generic.xaml 文件设置自定义控件的样式/主题。

  1. 使用 generic.xaml 时要记住的事项:
    • 资源必须命名为 generic.xaml 并位于名为 Themes 的文件夹中(案例很重要)
    • AssemblyInfo.cs 需要 ThemeInfo 属性
      • [程序集:ThemeInfo(ResourceDictionaryLocation.SourceAssembly,ResourceDictionaryLocation.SourceAssembly)]

每当有人使用它时,此主题将成为默认主题。

于 2012-01-11T20:05:06.233 回答