WPF不适用于MergedDictionaries定义的默认样式? [英] WPF not applying default styles defined in MergedDictionaries?
问题描述
在一个WPF应用程序,我定义在单独的资源字典默认控件样式(如ButtonStyle.xaml),并增加它们作为合并词典名为ResDictionary.xaml资源字典
如果我指的这个ResDictionary.xaml作为合并词典在我App.xaml中,默认的样式不适用。但是,如果我提到了ButtonStyle.xaml,它工作正常。
如果我重新编译相同的code。在.NET 3.5或3.0,它承认并应用到ResDictionary.xaml中提到的App.xaml中默认样式,而不是在.NET 4.0中。
在运行时,如果我签了Application.Current.Resources字典,默认样式是有的,但他们不适用,只有当我在Button控件显式地指定样式属性。
是否有任何解决方案,是指资源字典(containig默认样式),这种方式在.NET 4.0中?
App.xaml中:
< Application.Resources>
<的ResourceDictionary>
< ResourceDictionary.MergedDictionaries>
< ResourceDictionary中源=样式/ ResDictionary.xaml/>
< /ResourceDictionary.MergedDictionaries>
< / ResourceDictionary中>
< /Application.Resources>
ResDictionary.xaml:
< ResourceDictionary中的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml>
< ResourceDictionary.MergedDictionaries>
< ResourceDictionary中源=默认/ ButtonStyle.xaml/>
< /ResourceDictionary.MergedDictionaries>
< / ResourceDictionary中>
ButtonStyle.xaml:
< ResourceDictionary中的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml>
<风格的TargetType =按钮>
< setter属性=背景VALUE =黄色/>
< /样式和GT;
< / ResourceDictionary中>
有一个修复这个排序的,但我只能够使它在窗口的水平(而不是应用程序级别)的工作。
为了包括来自一个单独的项目一个WPF 4.0的资源,该资源必须添加为在窗口的的$ C $后面℃的资源。该声明属于窗口的构造,之前InitializeComponent方法调用:
公共ControlsWindow()
{
this.Resources = Application.LoadComponent(新的URI([WPF 4.0 ResourceProjectName];组件/ [在项目目录和文件名],UriKind.Relative))为ResourceDictionary中;
的InitializeComponent();
}
请注意:更换[WPF 4.0 ResourceProjectName]你的资源的项目名称的文本。此外,[在项目目录和文件名]需要更换的资源文件的相对位置(如主题/ StandardTheme.xaml')
我进入更详细信息有关此问题的这里。
In a WPF application I defined default control styles in separate resource dictionaries (e.g. "ButtonStyle.xaml"), and added them as merged dictionaries to a resource dictionary named "ResDictionary.xaml".
If I refer this "ResDictionary.xaml" as merged dictionary in my App.xaml, the default styles are not applied. However, if I refer the "ButtonStyle.xaml", it works correctly.
If I recompile the same code in .NET 3.5 or 3.0, it recognizes and applies the default styles referred in "App.xaml" through "ResDictionary.xaml", but not in .NET 4.0.
At runtime if I check the Application.Current.Resources dictionary, the default styles are there, but they are not applied, only if I specify the Style property explicitly in the Button control.
Are there any solutions to refer a resource dictionary (containig default styles) this way in .NET 4.0?
App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/ResDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
ResDictionary.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Default/ButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
ButtonStyle.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Button">
<Setter Property="Background" Value="Yellow"/>
</Style>
</ResourceDictionary>
There is a sort-of fix for this, but I’ve only been able to make it work at the window level (not the application level).
In order to include a WPF 4.0 resource from a separate project, the resource must be added as a resource in the window’s code behind. The statement belongs in the window’s constructor, prior to the InitializeComponent method call:
public ControlsWindow()
{
this.Resources = Application.LoadComponent(new Uri("[WPF 4.0 ResourceProjectName];Component/[Directory and File Name within project]", UriKind.Relative)) as ResourceDictionary;
InitializeComponent();
}
Note: Replace the '[WPF 4.0 ResourceProjectName]' text with your resource's project name. Also, the '[Directory and File Name within project]' needs to be replaced with the relative location of the resource file (like 'Themes/StandardTheme.xaml')
I go into more details about this issue here.
这篇关于WPF不适用于MergedDictionaries定义的默认样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!