【问题标题】:Why the WPF Window always get Default Style?为什么 WPF 窗口总是获得默认样式?
【发布时间】:2013-08-02 17:55:49
【问题描述】:

我尝试在 XAML 中为 WPF Window 设置 Style。我可以在 VS Designer 中看到我的更改,但是当我运行应用程序时,它总是会得到默认的 Style

不工作:

<Style TargetType="Window">
    <Setter Property="Background" Value="Red"/>
</Style>

如果我给 Style 加上 Key 并将 Style 应用到 Window 那么它就可以工作了。

工作:

<Style x:Key="window" TargetType="Window">
    <Setter Property="Background" Value="Red"/>
</Style>

有什么理由需要为Style 提供Window 的密钥?

谁能解释一下是怎么回事?

【问题讨论】:

    标签: wpf styles window


    【解决方案1】:

    需要在Window中添加构造:

    Style="{StaticResource {x:Type Window}}"
    

    样式在文件App.xaml:

    <Application x:Class="WindowStyleHelp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    
        <Application.Resources>
            <!-- In this case, the key is optional --> 
            <Style x:Key="{x:Type Window}" TargetType="{x:Type Window}">
                <Setter Property="Background" Value="Pink" />
            </Style>
        </Application.Resources>
    </Application>
    

    XAML 中的窗口:

    <Window x:Class="WindowStyleHelp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        Style="{StaticResource {x:Type Window}}"
        WindowStartupLocation="CenterScreen">
    
        <Grid>
    
        </Grid>
    </Window>
    

    【讨论】:

      【解决方案2】:

      试试

      <Style TargetType="{x:Type Window}">
          <Setter Property="Background" Value="Red"/>
      </Style>
      

      【讨论】:

      • 它与 OP 在他的问题中发布的内容有何不同?
      【解决方案3】:

      样式中的目标类型不会应用于派生类型。

      您可以使用StaticResource 在所有窗口上应用密钥-

      <Application x:Class="WpfApplication4.App"
                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                   xmlns:local="clr-namespace:WpfApplication4"
                   StartupUri="MainWindow.xaml">
          <Application.Resources>
              <Style x:Key="MyStyle" TargetType="Window">
                  <Setter Property="Background" Value="Red"/>
              </Style>
          </Application.Resources>
      </Application>
      
      <Window x:Class="WpfApplication1.MainWindow"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
              Style="{StaticResource MyStyle}">
      

      在这样的资源中定义style for your type (derived window) -

      <Application x:Class="WpfApplication4.App"
                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                   xmlns:local="clr-namespace:WpfApplication1"
                   StartupUri="MainWindow.xaml">
          <Application.Resources>
              <Style TargetType="{x:Type local:MainWindow}">
                  <Setter Property="Background" Value="Red"/>
              </Style>
          </Application.Resources>
      </Application>
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-11
      • 2011-03-09
      • 2017-06-13
      • 1970-01-01
      • 2010-12-30
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      相关资源
      最近更新 更多