wpf Trigger
1. 属性触发器Property Trigger:当Dependency Property的值发生改变时触发。
· 2. 数据触发器Data Trigger: 当普通.NET属性的值发生改变时触发。
· 3. 事件触发器Event Trigger: 当路由时间被触发时调用。
4. MultiDataTrigger & MultiTrigger
trigger主要运用的场景在Style、ControlTemplate、DataTemplate三个地方。
<Style x:Key="buttonMouseOver" TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="RenderTransform">
<Setter.Value>
<RotateTransform Angle="10"></RotateTransform>
</Setter.Value>
</Setter>
<Setter Property="RenderTransformOrigin" Value="0.5,0.5"></Setter>
<Setter Property="Background" Value="#FF0CC030" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type ListBoxItem}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=Role}" Value="Admin"> <Setter Property="Foreground" Value="Red" /> </DataTrigger> </Style.Triggers> </Style> |
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<ThicknessAnimation Storyboard.TargetProperty="Margin" Duration="0:0:0.5" From="50,50,50,50" To="0,0,50,50" AutoReverse="True" /> <DoubleAnimation Storyboard.TargetName="myDropShadowBitmapEffect" Storyboard.TargetProperty="ShadowDepth" From="0" To="30" Duration="0:0:0.5" AutoReverse="True" />
<DoubleAnimation Storyboard.TargetName="myDropShadowBitmapEffect" Storyboard.TargetProperty="Softness" From="0" To="1" Duration="0:0:0.5" AutoReverse="True" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
<Style TargetType="{x:Type Button}" x:Key="highlightStyle"> <Style.Triggers> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Background" Value="#EEEEEE" /> </Trigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="Visibility " Value="Visible" /> <Condition Property="IsEnabled" Value="true" /> </MultiTrigger.Conditions> <Setter Property="BorderBrush" Value="Red"/> <Setter Property="FontSize" Value="14" /> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="Foreground" Value="Red" /> </MultiTrigger> </Style.Triggers> </Style> … <Button Style="{StaticResource highlightStyle}" Content="Hight Value" x:Name="btnVisible" Click="Button_Click" /> |