当应用程序主题不是默认主题时,如何将WPF自定义控件与应用程序主题匹配?
我正在制作一个从 ComboBox 派生的自定义控件,并且在主题化方面遇到一些困难。我可以毫无困难地让它采用当前的操作系统主题,但是当我在 App.xaml 中显式加载新主题时,我的派生控件仍然显示操作系统主题。
我可以用最少的代码集重现该问题:
public class MyComboBox : ComboBox
{
static MyComboBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyComboBox), new FrameworkPropertyMetadata(typeof(MyComboBox)));
}
}
然后在 Themes/generic.xaml 中:
<Style TargetType="local:MyComboBox" BasedOn="{StaticResource {x:Type ComboBox}}">
</Style>
到目前为止一切顺利 - MyComboBox 显示正确,并与当前主题匹配。
但是,当我添加到我的 App.xaml 时:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/PresentationFramework.Luna;V3.0.0.0;31bf3856ad364e35;component\themes/luna.normalcolor.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
...常规 ComboBox 将显示 Luna chrome,但 MyComboBox 将继续显示当前主题(在我的情况下是 Aero,但我也用 Classic 进行了测试)。
我尝试添加
我已经确认它是从 aero.normalcolor.xaml (我的操作系统正在使用的主题)而不是从 luna.normalcolor.xaml (我在 App.xaml 中指定的主题)加载样式。
有人能给我指出正确的方向吗?或者我想做的事情是不可能的?谢谢!
I'm making a custom control derived from ComboBox, and I'm having some difficulty with theming. I'm able to get it to adopt the current OS theme without any difficulty, but when I explicitly load a new theme in my App.xaml, my derived control still displays with the OS theme.
I can reproduce the problem with a minimal set of code:
public class MyComboBox : ComboBox
{
static MyComboBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyComboBox), new FrameworkPropertyMetadata(typeof(MyComboBox)));
}
}
And then in Themes/generic.xaml:
<Style TargetType="local:MyComboBox" BasedOn="{StaticResource {x:Type ComboBox}}">
</Style>
So far so good -- MyComboBox displays correctly, and matches the current theme.
But then when I add to my App.xaml:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/PresentationFramework.Luna;V3.0.0.0;31bf3856ad364e35;component\themes/luna.normalcolor.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
...a regular ComboBox will display Luna chrome, but MyComboBox will continue to display the current theme (Aero, in my case, but I've tested with Classic too).
I've tried adding the <Style> tag for MyComboBox to Themes/luna.normalcolor.xaml and the other theme XAML files, with no effect -- only the <Style> from generic.xaml is loaded, no matter what theme I've specified in App.xaml. (mistake with ThemeInfoAttribute in my test app).
I've confirmed that it's loading the Style from aero.normalcolor.xaml (the theme my OS is using) rather than from luna.normalcolor.xaml (the theme I've specified in my App.xaml).
Can anybody point me the right direction? Or is what I'm trying to do not possible? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否为 AssemblyInfo.cs 中的 ThemeInfo 属性指定了正确的设置?
Did you specify the correct setings for the ThemeInfo attribute is the AssemblyInfo.cs?
这只是一个猜测,但是如果您使用 DynamicResource 呢?
That's just a guess, but what if you used DynamicResource instead?