wpf ResourceDictionary 中的 Windows 样式不适用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15428417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows Style from ResourceDictionary don't apply
提问by Walter Fabio Simoni
As I have multiple Windows in my application, I am looking for a solution that does not require me to set a bindingon each Window.
由于Window我的应用程序中有多个s,我正在寻找一种不需要我binding在每个Window.
I created a ResourceDictionarywhich has a stylefor the Window Background:
我为窗口背景创建了ResourceDictionary一个style:
<Style TargetType="{x:Type Window}">
<Setter Property="Background" Value="AliceBlue"/>
</Style>
In my XAML, I set the ResourceDictionary:
在我的 中XAML,我设置了ResourceDictionary:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Templates.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
There is no error, but my Windowcolor stays white.
没有错误,但我的Window颜色保持白色。
采纳答案by Taimoor Choudhary
Add a new brush in your resource dictionary
在您的资源字典中添加一个新画笔
<SolidColorBrush x:Key="WindowBackground" Color="AliceBlue" />
and in your WPF window simply set the required resource to the window background property
并在您的 WPF 窗口中简单地将所需资源设置为窗口背景属性
<Window x:Class="GDD.Presentation.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="300"
Background="{StaticResource WindowBackground}">
回答by Dan Puzey
This appears to be caused by a combination of the order in which WPF loads/processes styles from nested ResourceDictionary, and the specifics of the Windowclass.
这似乎是由 WPF 从 Nested 加载/处理样式的顺序ResourceDictionary以及Window类的细节的组合引起的。
Assume MainWindowis defined as per your post. Now put the following in Templates.xaml:
假设MainWindow是根据您的帖子定义的。现在将以下内容放入Templates.xaml:
<Style TargetType="{x:Type Window}">
<Setter Property="Background" Value="Red"/>
</Style>
<Style TargetType="{x:Type Window}" x:Key="myStyle">
<Setter Property="Background" Value="Green"/>
</Style>
If MainWindowhas no style defined, then you will see that in the designer it appears with a red background. The designer is parsing the whole Xaml and loading the resource dictionary, and then drawing the results. The style is read before the window is drawn, and so the red background is applied.
如果MainWindow没有定义样式,那么您将在设计器中看到它以红色背景显示。设计者正在解析整个Xaml并加载资源字典,然后绘制结果。在绘制窗口之前读取样式,因此应用红色背景。
When you run the application, the window is created before the ResourceDictionaryis applied. It looks for a default style (a style with x:Key="{x:Type Window}") beforethe nested ResourceDictionaryis processed, and finds nothing. Therefore at runtime, the window appears with default colour. (This is the behaviour described in the comments above.) Remember that the style with x:Key="{x:Type Window}"has a default value that matches the Windows style.
运行应用程序时,会在应用 之前创建窗口ResourceDictionary。它在处理嵌套之前查找默认样式(带有 的样式x:Key="{x:Type Window}"),但ResourceDictionary一无所获。因此,在运行时,窗口以默认颜色显示。(这是上面评论中描述的行为。)请记住,样式具有x:Key="{x:Type Window}"与 Windows 样式匹配的默认值。
This is borne out if you use myStyleexplicitly. If you add to your Windowdefinition the attribute Style="{StaticResource myStyle}"you'll find that the designer fails, but you also get a run-time error, because myStylehasn't been created at the time that the Window needs it. If you switch to Style="{DynamicResource myStyle}"then you'll see that it works as you hope, because DynamicResourcewill update once the ResourceDictionaryhas been parsed and the style included.
如果您myStyle明确使用,则可以证明这一点。如果您将Window属性添加到您的定义中,Style="{StaticResource myStyle}"您会发现设计器失败,但您也会收到运行时错误,因为myStyle在 Window 需要它时尚未创建。如果您切换到Style="{DynamicResource myStyle}"那么您会看到它按您希望的方式工作,因为DynamicResource一旦ResourceDictionary解析并包含样式就会更新。
So, applying this, you can fix the problem in one way by adding this to your Window element: Style="{DynamicResource {x:Type Window}}"- but this is cludgy. The better solution is to include your resource dictionary in the app.xamlfile, where it will be parsed before any window is opened and thus available to all:
因此,应用此方法,您可以通过将其添加到 Window 元素来以一种方式解决问题:Style="{DynamicResource {x:Type Window}}"- 但这很笨拙。更好的解决方案是将您的资源字典包含在app.xaml文件中,在打开任何窗口之前将对其进行解析,从而可供所有人使用:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Templates.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
The real issue here is that your Windowis not really a Window: it is a class that derives from Windowand will in fact be MainWindow, Window2, etc... This means that the automatic style wireup for a Windowwill neverwork in this way, and some level of manual binding will unfortunately always be required.
这里真正的问题是,你Window是不是一个真正的Window:它是一个类,派生自Window并实际上将MainWindow,Window2等...这意味着自动样式wireup一个Window将永远不会以这种方式工作,和一定程度的不幸的是,总是需要手动装订。
回答by tdr
This is the solution I used in my application. It lets me keep all my window styles together, and requires just a couple lines after the <Window.Resources>section.
这是我在我的应用程序中使用的解决方案。它让我可以将所有窗口样式保持在一起,并且在该<Window.Resources>部分之后只需要几行。
Do your Stylelike so:
做你Style喜欢这样:
<Style x:Key="MyWindowStyle">
<Setter Property="Window.Background" Value="AliceBlue"/>
</Style>
Then, in your Window, after </Window.Resources>include the following:
然后,在您的窗口中,</Window.Resources>包含以下内容:
<Window.Style>
<Style BasedOn="{StaticResource MyWindowStyle}"/>
</Window.Style>

