简体   繁体   English

使用XAML设置窗口背景颜色

[英]Setting window background color with XAML

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. 但是在Visual Studio的设计器选项卡中,您可以在其中看到窗口的预览,背景颜色正确更改为红色。 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? 我在这里做错了什么?


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>

OK... So this is because your window is actually a type deriving from Window... 好...这是因为您的窗口实际上是衍生自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. 希望这会有所帮助...据我所知,没有更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM