将控件样式与另一个项目动态设置的全局样式合并
我目前面临一个问题。我正在使用在 codeplex 上找到的 WPF.Themes,它允许我更改应用程序的主题。
因此,我导入了该项目并使其一切正常,但对于某些控制(例如我的 treeViewItem),我已经设置了样式,它会覆盖全局样式。
经过研究,我有以下代码,但仍然无法工作。
<TreeView Name="_tvTreeView" Grid.Row="1" >
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}">
<EventSetter Event="MouseDoubleClick" Handler="tvTreeView_PreviewMouseDoubleClick"/>
<EventSetter Event="MouseDown" Handler="tvTreeView_MouseDown"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
如果我在主项目的 app.xaml 的合并字典中手动添加 resrouce 文件,则基于的工作。
但是 WPF.Themes 项目允许我通过这样做动态更改主题。
public static void ApplyTheme(this ContentControl control, string theme)
{
ResourceDictionary dictionary = ThemeManager.GetThemeResourceDictionary(theme);
control.Resources.MergedDictionaries.Clear();
if (dictionary != null)
control.Resources.MergedDictionaries.Add(dictionary);
}
有了上面的代码,不会合并全局样式和我的事件设置器。 如果我手动引用 app.xaml 中的主题,那么“BasedOn”将启动并工作,但如果我动态设置 mergedDictionaries,“BasedOn”似乎不起作用。
有没有一种方法可以让我在不将主题添加到 app.xaml 的情况下使其工作。
谢谢和问候,
I currently am facing a problem. I am using WPF.Themes which i found on codeplex, it allows me to change my application's theme.
So I imported the project and got it all working fine, but for some control, say my treeViewItem, I had style already set to it which it overrides the global styles.
I have the following code after research but still won't work.
<TreeView Name="_tvTreeView" Grid.Row="1" >
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}">
<EventSetter Event="MouseDoubleClick" Handler="tvTreeView_PreviewMouseDoubleClick"/>
<EventSetter Event="MouseDown" Handler="tvTreeView_MouseDown"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
The based on works if I manully add the resrouce file in the merge dictionary of app.xaml of my Main Project.
But WPF.Themes project allow me to change theme dynamically by doing this.
public static void ApplyTheme(this ContentControl control, string theme)
{
ResourceDictionary dictionary = ThemeManager.GetThemeResourceDictionary(theme);
control.Resources.MergedDictionaries.Clear();
if (dictionary != null)
control.Resources.MergedDictionaries.Add(dictionary);
}
Having the above code, does not merge the global styles and my event setters.
If I manually reference the theme in app.xaml then "BasedOn" would kick in and work, but "BasedOn" don't seem to work if I set the mergedDictionaries dynamically.
Is there a way I can get this to work without adding the theme to app.xaml.
Thanks and Regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
style的BaseOn属性不能用DynamicResource设置,用StaticResource设置,应用于控件时会被密封。
您应该在全局样式更改时合并样式,请尝试以下代码:
将附加属性 AutoMergeStyle 添加到 xaml:
The BaseOn property of style cannot be set with DynamicResource, with StaticResource, it will be sealed when applied to control.
You should merge the style when global style changed, try these codes:
Add attached property AutoMergeStyle to xaml: