wpf 使用 XAML 设置窗口背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23783813/
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
Setting window background color with XAML
提问by user3595338
I have a standard XAML styled window in WPF (< Window ....)
我在 WPF 中有一个标准的 XAML 样式的窗口(< Window ....)
In this window I insert a resource dictionary
在这个窗口中,我插入了一个资源字典
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style/Global.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
In the Global.xaml dicionary I have the following code:
在 Global.xaml 字典中,我有以下代码:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Window">
<Setter Property="Background" Value="Red"/>
</Style>
</ResourceDictionary>
Nothing out of ordinary anywhere. Except it doesn't work, when I compile and run the app, the window background is shown in default white. BUT in the designer tab in Visual Studio where you can see the preview of your window, the background color is correctly changed to red. I don't understand. I don't have any other styles inserted anywhere that could be overwriting the window's background color. So how is it possible that in the preview tab it works correctly, but when i actually run the app, it doesn't? What am I doing wrong here?
任何地方都没有异常。除了它不起作用之外,当我编译并运行应用程序时,窗口背景以默认白色显示。但是在 Visual Studio 的设计器选项卡中,您可以在其中看到窗口的预览,背景颜色已正确更改为红色。我不明白。我没有在任何可能覆盖窗口背景颜色的地方插入任何其他样式。那么怎么可能在预览选项卡中它可以正常工作,但是当我实际运行该应用程序时,它却没有?我在这里做错了什么?
Here is the entire window code:
这是整个窗口代码:
<Window x:Class="Apptest.EditBook"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="EditBook" Height="300" Width="300">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style/Global.xaml" />
<ResourceDictionary Source="Style/Controls.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
</Grid>
</Window>
回答by BenjaminPaul
OK... So this is because your window is actually a type deriving from Window...
好的...所以这是因为您的窗口实际上是从窗口派生的类型...
public partial class EditBook : Window { }
Target type does not yet work with derived types so you will need to add a key to the style and add it to each window you create that you want to use the style for..
目标类型尚不能与派生类型一起使用,因此您需要为样式添加一个键,并将其添加到您创建的要使用该样式的每个窗口中。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Window" x:Key="MyWindowStyle">
<Setter Property="Background" Value="Red"/>
</Style>
</ResourceDictionary>
then you will need to apply the style in the window...
那么您将需要在窗口中应用样式...
<Window x:Class="Apptest.EditBook"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="EditBook" Height="300" Width="300" Style="{StaticResource MyWindowStyle}">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style/Global.xaml" />
<ResourceDictionary Source="Style/Controls.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
</Grid>
</Window>
Hope this helps... there is no better solution from what I can see.
希望这会有所帮助...从我所见,没有更好的解决方案。

