未应用WPF窗口样式应如何解决?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (38)

我有一个ResourceDictionary,它包含我的应用程序中使用的控件的样式定义。

所有样式都正确地应用于窗口中的控件,但ResourceDictionary中窗口本身的样式未应用。

这是我的ResourceDictionary中的XAML,它包含我想要应用于我的窗口的样式:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:primatives="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type Window}">
        <Setter Property="Background" Value="#FF121212"></Setter>
        <Setter Property="Height" Value="768"></Setter>
        <Setter Property="Width" Value="1024"></Setter>
    </Style>
<!-- .... -->
</ResourceDictionary>

这是我正在使用的窗口的XAML(试图让这个样式适用):

<Window x:Class="TryingStyles"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TryingStyles">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/StylesDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>    
    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="Label1" VerticalAlignment="Top" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="56,14,0,0" Name="TextBox1" VerticalAlignment="Top" Width="120" />
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TabControl Height="206" HorizontalAlignment="Left" Margin="12,43,0,0" Name="TabControl1" VerticalAlignment="Top" Width="250">
                <TabItem Header="TabItem1" Name="TabItem1">
                    <Grid></Grid>
                </TabItem>
            </TabControl>
            <GroupBox Header="GroupBox1" Margin="268,43,12,12" Width="396"></GroupBox>
        </StackPanel>
    </StackPanel>
</Window>

看来,当我在IDE的“设计视图”中查看窗口时,将应用窗口的样式,但是当我运行该应用程序时,该样式未应用。原因是什么?应如何解决?

提问于
用户回答回答于

这里有两种选择:你可以在你的风格中添加一个键,并将样式应用到所有的Windows中。如下:

    <!-- Resource file -->    
    <ResourceDictionary ...>
        <Style TargetType="{x:Type Window}" x:Key="WindowDefaultStyle">
            <!-- .... -->    
        </Style>
    </ResourceDictionary>

    <!-- Window file -->
    <Window Style="{DynamicResource ResourceKey=WindowDefaultStyle}">

或者你可以使用Style的BasedOn属性,代码:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:my="clr-namespace:WpfApplication1">
    <Style TargetType="{x:Type Window}" x:Key="BaseStyle">
        <Setter Property="Background" Value="#FF121212"></Setter>
        <Setter Property="Height" Value="768"></Setter>
        <Setter Property="Width" Value="1024"></Setter>
    </Style>

    <!-- Inherit from the BaseStyle and define for the MainWindow class -->
    <Style TargetType="{x:Type my:MainWindow}" BasedOn="{StaticResource ResourceKey=BaseStyle}" />
</ResourceDictionary>
用户回答回答于

Wpf似乎无法将Window类与派生类TryingStyles进行匹配。改变你的TargetType,它会起作用:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:primatives="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:my="clr-namespace:WpfApplication1">
    <Style TargetType="{x:Type my:TryingStyles}">
        <Setter Property="Background" Value="#FF121212"></Setter>
        <Setter Property="Height" Value="768"></Setter>
        <Setter Property="Width" Value="1024"></Setter>
    </Style>
    <!-- .... -->
</ResourceDictionary>

扫码关注云+社区

领取腾讯云代金券