WPF窗口样式未应用

时间:2010-11-25 18:00:52

标签: wpf styles

我有一个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的“设计视图”中查看窗口时,似乎应用了窗口的样式,但是当我运行应用程序时,未应用样式。

有谁知道我做错了什么?

2 个答案:

答案 0 :(得分:24)

似乎没有适当的解决方案来解决您的问题。样式中的TargetType不管理派生类型。 以下是两种选择: 您可以在您的样式中放置一个键,并将样式应用于所有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>

答案 1 :(得分:3)

很奇怪,它适用于设计师,但不适用于应用程序运行时。 问题似乎是你的Style的TargetType。 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>