样式(Style类)用于给控件定义外观,样式包含一个或多个 Setter对象的集合,每个 Setter由 Property和 Value组成。
样式也是一种资源,可以像引用任何其他资源一样对其进行引用。
官方文档:https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.style?view=netframework-4.8
Style类提供一个WPF资源对象,其部分属性如下:
属性 | 说 明 |
---|---|
TargetType | 获取或设置此样式所针对的类型。 |
BasedOn | 获取或设置一个作为当前样式的基准的已定义样式 |
Setters | 获取 Setter 和 EventSetter 对象的集合, |
Triggers | 获取基于指定条件应用属性值的 TriggerBase 对象的集合.。 |
Resources | 获取或设置可在此样式的范围内使用的资源的集合。 |
Setter提供一个设置属性的值。
属性 | 说 明 |
---|---|
Property | 获取或设置要应用 Value 的属性。 |
Value | 获取或设置属性的值。 |
TargetName | 获取或设置此 Setter 所用于的元素x:Name的名称。仅在 ControlTemplate的Triggers中的setter生效 |
<Window.Resources>
<Style x:Key="myKey" TargetType="Control">
"Background" Value="Red"/>
"Foreground" Value="White"/>
"FontSize" Value="20"/>
Style>
<Style TargetType="TextBox">
"BorderBrush" Value="blue"/>
"BorderThickness" Value="4"/>
"FontSize" Value="15"/>
Style>
<Style x:Key="myKey2" TargetType="TextBox" BasedOn="{StaticResource myKey}">
"BorderBrush" Value="Black"/>
"BorderThickness" Value="4"/>
"FontSize" Value="22"/>
Style>
<Style x:Key="myKey3" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
"BorderBrush" Value="GreenYellow"/>
"Background" Value="Yellow"/>
Style>
Window.Resources>
<Grid>
<Button x:Name="button1" Content="Button" Style="{StaticResource myKey}" HorizontalAlignment="Left" Margin="198,69,0,0" VerticalAlignment="Top" Height="40" Width="120"/>
<Button x:Name="button2" Content="Button" Style="{DynamicResource myKey}" HorizontalAlignment="Left" Margin="198,159,0,0" VerticalAlignment="Top" Height="40" Width="120"/>
<Button x:Name="button3" Content="Button" HorizontalAlignment="Left" Margin="198,249,0,0" VerticalAlignment="Top" Height="40" Width="120"/>
<TextBox x:Name="textBox1" Style="{StaticResource myKey}" TextAlignment="Center" HorizontalAlignment="Left" Margin="400,69,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" Height="40"/>
<TextBox x:Name="textBox2" Style="{StaticResource myKey2}" HorizontalAlignment="Left" TextAlignment="Center" Margin="400,159,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" Height="40"/>
<TextBox x:Name="textBox3" Style="{StaticResource myKey3}" HorizontalAlignment="Left" Margin="400,249,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" Height="40"/>
<TextBox x:Name="textBox4" TextAlignment="Center" HorizontalAlignment="Left" Margin="608,69,0,0" TextWrapping="Wrap" Text="TextBox4" VerticalAlignment="Top" Width="120" Height="40"/>
Grid>
EventSetter 提供一个设置事件的值。
属性 | 说 明 |
---|---|
Event | 获取或设置此 EventSetter 响应的特定路由事件。 |
Handler | 获取或设置对资源库中路由事件的处理程序的引用。 |
<Window.Resources>
<Style TargetType="Button">
"Click" Handler="Button_Click"/>
"MouseEnter" Handler="Button_MouseEnter"/>
Style>
<Style TargetType="TextBox">
"TextChanged" Handler="TextBox_TextChanged"/>
Style>
Window.Resources>
<Grid>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="284,69,0,0" VerticalAlignment="Top" Height="88" Width="247"/>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="112" Margin="284,201,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="247"/>
Grid>
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("按钮被点击");
}
private void Button_MouseEnter(object sender, MouseEventArgs e)
{
MessageBox.Show("鼠标移入");
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox tb = (TextBox)sender;
Console.WriteLine("文件框内容为:" + tb.Text);
}