C# Trigger、MultiTrigger和DataTrigger的简要用法
下面列出一个简要的例子:
1 <Style TargetType="TextBox" x:Key="basestyle"> 2 <Setter Property="FontSize" Value="35"></Setter> 3 <Setter Property="Foreground" Value="#865FC5"></Setter> 4 <Style.Triggers> 5 <!--①Trigger--> 6 <Trigger Property="IsMouseOver" Value="true" > 7 <Setter Property="FontSize" Value="35"></Setter> 8 <Setter Property="Foreground" Value="DarkRed"></Setter> 9 </Trigger> 10 <Trigger Property="IsMouseOver" Value="false" > 11 <Setter Property="FontSize" Value="25"></Setter> 12 <Setter Property="Foreground" Value="DarkBlue"></Setter> 13 </Trigger> 14 <!--②MultiTrigger--> 15 <MultiTrigger> 16 <MultiTrigger.Conditions> 17 <Condition Property="IsMouseOver" Value="true"></Condition> 18 <Condition Property="IsFocused" Value="true"></Condition> 19 </MultiTrigger.Conditions> 20 <MultiTrigger.Setters> 21 <Setter Property="FontStyle" Value="Oblique"></Setter> 22 <Setter Property="FontWeight" Value="3"></Setter> 23 </MultiTrigger.Setters> 24 </MultiTrigger> 25 <!-- ③DataTrigger--> 26 <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self},Path=Text }" Value="123"> 27 <Setter Property="Background" Value="Green"></Setter> 28 </DataTrigger> 29 </Style.Triggers> 30 </Style>
①:普通触发器
此触发器设置了两个,一个是鼠标放在control上显示暗红色,一个是鼠标离开显示蓝色,效果如下图:
鼠标放上:
鼠标离开:
②:多条件触发器,此条件设置为鼠标放在其上而且组件被选中时字体为斜体
③:数据触发器,此数据设置为数据为123时背景改为绿色