1.概要
wpf style 用来控制控件的样式
2.代码
<Window x:Class="WpfApp2.Window5"
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:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="Window5" Height="450" Width="800">
<Window.Resources>
<ResourceDictionary>
<!--设置控件样式-->
<Style TargetType="Button" x:Key="btnStyle">
<!--Setter设置控件静态属性-->
<Setter Property="FontSize" Value="16"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}" >
<Border x:Name="back" BorderBrush="{TemplateBinding Control.BorderBrush}" Background="Red" CornerRadius="5">
<!--ControlPresenter:内容占位符-->
<ContentPresenter Content="{TemplateBinding ContentControl.Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<!--Triggers设置控件的行为风格-->
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="back" Property="Background" Value="Orange"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Window.Resources>
<StackPanel>
<!--设置按钮圆角-->
<Button Content="样式1" Width="100" Style="{StaticResource btnStyle}" />
<Button Content="样式2" Width="100" Style="{StaticResource btnStyle}" />
</StackPanel>
</Window>
3.运行结果