wpf使⽤style设置样式
wpf中使⽤style
wpf中style
构成style最重要的两种元素: setter 和trigger
setter类帮助我们设置控件的静态外观风格,Trigger类帮助我们设置控件的⾏为风格
Setter
setter 类的Property属性⽤来指明你想为⽬标的哪个属性赋值;Setter类的Value属性则是你提供的属性值。如果在window的资源字典中放置⼀个针对TextBlock的style,style中使⽤若⼲Setter来设定TextBlock的⼀些属性,这样程序中的Text Block就会具有统⼀的风格,除⾮你使⽤{x:Null} 来显⽰的清空style
Trigger
trigger,即当某些条件满⾜时会触发⼀个⾏为。trigger也有Property和value 属性,property是trigger关注的属性名称,value是触发条件。trigger类还有⼀个setters属性,次属性值是⼀组setter,⼀旦触发条件被满⾜,这组setter的“属性–值”就会被应⽤,触发条件不再满⾜后,各属性值会被还原。
multitrigger
当多个条件同时满⾜时,触发器才会被触发。multiTrigger⽐Trigger多了⼀个conditions属性,需要同时满⾜的条件就存储在这个集合中。
DataTrigger
程序中经常会遇到基于数据执⾏某些判断情况,遇到这种情况时可以考虑使⽤Datatrigger。dataTrigger对象的Binding属性会把数据源源不断送过来,⼀旦送来的值与value属性⼀致,dataTrigger即被触发。
MultiDataTrigger
有时会遇到要求多个条件同时满⾜时才能触发的需求。
EventTrigger
eventTrigger是触发器中最特殊的⼀个。⾸先,它不是由属性或数据变化来触发⽽是由事件来触发;
其次,被触发后它并⾮应⽤⼀组setter,⽽是执⾏⼀段动画。因此,UI层的动画效果往往与EventTrigger 相关联
代码⽰例
代码下载地址
<Window x:Class="WpfApp11Style02.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:WpfApp11Style02"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="800">
<!--资源字典-->
<Window.Resources>
<Style TargetType="TextBlock">
<Style.Setters><!--此⾏可省略-->
<Setter Property="FontSize" Value="24"/>
<Setter Property="TextDecorations" Value="Underline"/>
<Setter Property="FontStyle" Value="Italic"/>
<Setter Property="FontStyle" Value="Italic"/>
</Style.Setters>
</Style>
<!--<Style TargetType="CheckBox">
<Style.Triggers>
<Trigger Property="IsChecked" Value="true">
<Trigger.Setters>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Foreground" Value="Orange"/>
</Trigger.Setters>
</Trigger>
</Style.Triggers>
</Style>-->
<Style TargetType="CheckBox">
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsChecked" Value="true"/>
<Condition Property="Content" Value="我挥⼀挥⾐袖"/>
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Foreground" Value="Orange"/>
</MultiTrigger.Setters>
</MultiTrigger>
</Style.Triggers>
</Style>
<local:L2BConvert x:Key="cvtr"/>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource=
{x:Static RelativeSource.Self},Path=Text.Length,
Converter={StaticResource cvtr}}" Value="false">
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="BorderThickness" Value="1"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Style TargetType="ListBoxItem">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ID}" Width="60"/>
<TextBlock Text="{Binding Name}" Width="120"/>
<TextBlock Text="{Binding Age}" Width="60"/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
<!--multiDataTrigger-->
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=ID}" Value="2"/>
<Condition Binding="{Binding Path=Name}" Value="Tom"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<MultiDataTrigger.Setters>
<Setter Property="Background" Value="Orange"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
<!--eventTrigger-->
<Style TargetType="Button">
<Style.Triggers>
<!--⿏标进⼊-->
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation  To="150" Duration="0:0:0.2" Storyboard.TargetProperty="Width"/>
<DoubleAnimation To="150" Duration="0:0:0.2" Storyboard.TargetProperty="Height"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<!--⿏标离开-->
<EventTrigger RoutedEvent="MouseLeave">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="Width"/>
<DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="Height"/>
textstyle
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<!--窗体-->
<StackPanel Margin="5">
<TextBlock Text="Hello WPF!"/>
<TextBlock Text=" This is a sample for Style"/>
<!--不使⽤style-->
<TextBlock Text="by Tim 2009.12.23" Style="{x:Null}"/>
<CheckBox Content="悄悄地我⾛了" Margin="5"/>
<CheckBox Content="正如我悄悄的来" Margin="5,0"/>
<CheckBox Content="我挥⼀挥⾐袖" Margin="5"/>
<CheckBox Content="不带⾛⼀⽚云彩" Margin="5,0"/>
<TextBox Margin="5"/>
<TextBox Margin="5,0"/>
<TextBox Margin="5"/>
<ListBox x:Name="listBoxStudent" Margin="5"/>
<Button Width="40" Height="40" Content="OK"/>
</StackPanel>
</Window>
图⽚效果展⽰

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