未应用 WPF 窗口样式 [英] WPF window style not being applied

查看:26
本文介绍了未应用 WPF 窗口样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a ResourceDictionary that contains Style definitions for controls used within my application.

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

All of the styles are applied properly to the controls in the window...but the style in the ResourceDictionary for the window itself is not being applied.

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

This is the XAML in my ResourceDictionary that contains the style that I want to apply to my window:

<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(尝试应用此样式):

This is the XAML for the window that I am working with (trying to get this style to apply):

<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 的设计视图"中查看窗口时,似乎应用了该窗口的样式,但当我运行该应用程序时,该样式并未应用.

It appears that the style for the window is applied when I view the window in the IDE's "Design view" but when I run the application the style is not applied.

有人知道我做错了什么吗?

Does anyone know what I'm doing wrong?

推荐答案

您的问题似乎没有合适的解决方案.Styles 中的 TargetType 不管理派生类型.这里有两种选择:您可以在您的样式中添加一个键并将该样式应用到您的所有 Windows.

It appears that there is no proper solution to your problem. TargetType in Styles doesn't manage derived types. Here are two alternatives : You can put a key in your style and apply the style to all your Windows.

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

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

或者您可以使用 Style 的 BasedOn 属性.

Or you can use the BasedOn property of the Style.

<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 窗口样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆