设置 WPF 样式
setting WPF style
我在 wpf 中写了一个 XAML 代码,我在 window.resourse 中定义了这样一个样式
<Window x:Class="WpfApp1.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:self="clr-namespace:WpfApp1"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:local ="clr-namespace:WpfApp1"
mc:Ignorable="d"
Height="400 " Width="450" >
<Window.Resources>
<Style TargetType="{x:Type Window}">
<Setter Property="Title" Value="Hello my friens!"/>
</Style>
</Window.Resources>
<Grid>
</Grid>
在这里,Test 是我的 window class 名字。当我 运行 一切正常但是当我将上面的内容更改为
<Window.Resources>
<Style TargetType="{x:Type Window}">
<Setter Property="Title" Value="Hello my friends!"/>
</Style>
</Window.Resources>
在设计 window 中,标题显示为 Value="Hello my friends!" 但是当我 运行 应用程序时,标题变为空。
这是怎么回事?
顺便说一下 TargetType="{x:Type Window}" 和 TargetType="{x:Type local:Test}"?
他们不是都提到了window类型吗?
只需在样式中指定目标类型,该样式就会自动应用于所有 objects 您为其定义的类型。但是,这不适用于基本 classes.
在您的示例中,TargetType="{x:Type Window}"
会自动将标题 "Hello my friends!" 应用到所有 windows。但是,你的 window 的类型不是 Window
,而是 WpfApp1.Test
。 Window 只是使用的基础 class。这就是样式不会自动应用于 window 的原因。
如果您改用 TargetType="{x:Type local:Test}"
,它将自动应用于所有类型为 WpfApp1.Test
的 objects,您的 window 也是如此。样式的自动应用仅适用于特定类型,不适用于基础 class.
你也可以指定一个键属性,然后告诉你的window它应该使用这个样式。在这种情况下,您还可以使用 x:Type Window
,因为那样会显式应用样式。
例如:
<Window x:Class="WpfApp1.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:self="clr-namespace:WpfApp1"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:local ="clr-namespace:WpfApp1"
mc:Ignorable="d"
Height="400 " Width="450" Style="{DynamicResource MyStyle}">
<Window.Resources>
<Style TargetType="{x:Type Window}" x:Key="MyStyle">
<Setter Property="Title" Value="Hello my friends!"/>
</Style>
</Window.Resources>
我在 wpf 中写了一个 XAML 代码,我在 window.resourse 中定义了这样一个样式
<Window x:Class="WpfApp1.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:self="clr-namespace:WpfApp1"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:local ="clr-namespace:WpfApp1"
mc:Ignorable="d"
Height="400 " Width="450" >
<Window.Resources>
<Style TargetType="{x:Type Window}">
<Setter Property="Title" Value="Hello my friens!"/>
</Style>
</Window.Resources>
<Grid>
</Grid>
在这里,Test 是我的 window class 名字。当我 运行 一切正常但是当我将上面的内容更改为
<Window.Resources>
<Style TargetType="{x:Type Window}">
<Setter Property="Title" Value="Hello my friends!"/>
</Style>
</Window.Resources>
在设计 window 中,标题显示为 Value="Hello my friends!" 但是当我 运行 应用程序时,标题变为空。 这是怎么回事? 顺便说一下 TargetType="{x:Type Window}" 和 TargetType="{x:Type local:Test}"?
他们不是都提到了window类型吗?
只需在样式中指定目标类型,该样式就会自动应用于所有 objects 您为其定义的类型。但是,这不适用于基本 classes.
在您的示例中,TargetType="{x:Type Window}"
会自动将标题 "Hello my friends!" 应用到所有 windows。但是,你的 window 的类型不是 Window
,而是 WpfApp1.Test
。 Window 只是使用的基础 class。这就是样式不会自动应用于 window 的原因。
如果您改用 TargetType="{x:Type local:Test}"
,它将自动应用于所有类型为 WpfApp1.Test
的 objects,您的 window 也是如此。样式的自动应用仅适用于特定类型,不适用于基础 class.
你也可以指定一个键属性,然后告诉你的window它应该使用这个样式。在这种情况下,您还可以使用 x:Type Window
,因为那样会显式应用样式。
例如:
<Window x:Class="WpfApp1.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:self="clr-namespace:WpfApp1"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:local ="clr-namespace:WpfApp1"
mc:Ignorable="d"
Height="400 " Width="450" Style="{DynamicResource MyStyle}">
<Window.Resources>
<Style TargetType="{x:Type Window}" x:Key="MyStyle">
<Setter Property="Title" Value="Hello my friends!"/>
</Style>
</Window.Resources>