WPF中的Style样式设置
1、使⽤Style设置控件的某些属性,并在指定的范围内起作⽤:
下⾯是样式定义:
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="FontSize" Value="25"/>
<Setter Property="FontWeight" Value="Heavy"/>
</Style>
</Window.Resources>
该样式的定义是作为⼀种资源被保存下来,对整个窗体中的所有Button起作⽤:
<Window x:Class="StyleDemo.MainWindow"
xmlns="schemas.microsoft/winfx/2006/xaml/presentation"
xmlns:x="schemas.microsoft/winfx/2006/xaml"
xmlns:d="schemas.microsoft/expression/blend/2008"
xmlns:mc="/markup-compatibility/2006"
xmlns:local="clr-namespace:StyleDemo"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="FontSize" Value="25"/>
<Setter Property="FontWeight" Value="Heavy"/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.Column="0" Content="button1" Margin="5"/>
<Button Grid.Row="0" Grid.Column="1" Content="button2" Margin="5"/>
</Grid>
</Window>
2、定义⼀种Style样式只对指定的Button对象起作⽤,为Style添加⼀个x:Key=“ButtonStyle”样式定义:
<Window.Resources>
<Style TargetType="Button" x:Key="ButtonStyle">
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="FontSize" Value="25"/>
<Setter Property="FontWeight" Value="Heavy"/>
</Style>
</Window.Resources>
此种定义⽅式,通过ButtonStyle只对指定的控件设置样式
<Window x:Class="StyleDemo.MainWindow"
xmlns="schemas.microsoft/winfx/2006/xaml/presentation"
xmlns:x="schemas.microsoft/winfx/2006/xaml"
xmlns:d="schemas.microsoft/expression/blend/2008"
xmlns:mc="/markup-compatibility/2006"
xmlns:local="clr-namespace:StyleDemo"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="Button" x:Key="ButtonStyle">
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="FontSize" Value="25"/>
<Setter Property="FontWeight" Value="Heavy"/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.Column="0" Content="button1" Margin="5"/>
<Button Style="{StaticResource ButtonStyle}" Grid.Row="0" Grid.Column="1" Content="button2" Margin="5"/>
</Grid>
</Window>
fontweight默认值3、定义⼀种Style可以对外界的交互做出响应,⽐如⿏标按下时字体颜⾊变化,释放⿏标颜⾊恢复。通过在Style中添加触发器Trigger设置;另外可以使⽤BaseOn“继承”其它的Style;
参考代码:
<Window.Resources>
<Style TargetType="Button" x:Key="ButtonStyle">
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="FontSize" Value="25"/>
<Setter Property="FontWeight" Value="Heavy"/>
</Style>
<Style TargetType="Button" x:Key="TriggerButton" BasedOn="{StaticResource ButtonStyle}">
<Style.Triggers>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。