未应用WPF窗口样式
我有一个ResourceDictionary,其中包含应用程序中使用的控件的样式定义 所有样式都已正确应用于窗口中的控件…但未应用窗口本身的ResourceDictionary中的样式 这是我的ResourceDictionary中的XAML,其中包含我要应用于我的窗口的样式:未应用WPF窗口样式,wpf,styles,Wpf,Styles,我有一个ResourceDictionary,其中包含应用程序中使用的控件的样式定义 所有样式都已正确应用于窗口中的控件…但未应用窗口本身的ResourceDictionary中的样式 这是我的ResourceDictionary中的XAML,其中包含我要应用于我的窗口的样式: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x
<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(尝试应用此样式):
当我在IDE的“设计视图”中查看窗口时,窗口的样式似乎已应用,但当我运行应用程序时,样式未应用
有人知道我做错了什么吗?很奇怪,它与设计器一起工作,但在应用程序运行时却不工作。 问题似乎在于你风格的目标类型。Wpf似乎无法将窗口类与派生的类样式相匹配 更改您的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>
您的问题似乎没有合适的解决方案。样式中的TargetType不管理派生类型。 这里有两种选择: 您可以在样式中放置一个键,并将样式应用于所有窗口
<!-- Resource file -->
<ResourceDictionary ...>
<Style TargetType="{x:Type Window}" x:Key="WindowDefaultStyle">
<!-- .... -->
</Style>
</ResourceDictionary>
<!-- Window file -->
<Window Style="{DynamicResource ResourceKey=WindowDefaultStyle}">
也可以使用样式的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>
虽然这样做有效,但我必须对应用程序中的每个窗口执行此操作。我希望有一个窗口样式可以应用于我应用程序中的所有窗口。非常感谢您的帮助Nicolas:)您还应该检查此解决方案:
<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>