WPF之触发器
简单触发器
<Window x:Class="WpfApp.Window1" 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:WpfApp" mc:Ignorable="d" Title="Window1" Height="450" Width="800"> <Window.Resources> <Style x:Key="BigFontButton"><!--命名为大字体--> <Setter Property="Control.FontFamily" Value=" Times new Roman"></Setter> <Setter Property="Control.FontSize" Value="32"></Setter> <Style.Triggers><!--触发器集合--> <Trigger Property="Control.IsFocused" Value="True"><!--获取焦点触发--> <Setter Property="Control.Foreground" Value="DarkRed"></Setter><!--触发内容为变红色--> </Trigger> </Style.Triggers> </Style> </Window.Resources> <Grid> <Button Content="Button" Style="{StaticResource BigFontButton}"/> </Grid> </Window>
结果button获取焦点后 字体变红
<Window x:Class="WpfApp.Window1" 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:WpfApp" mc:Ignorable="d" Title="Window1" Height="450" Width="800"> <Window.Resources> <Style x:Key="BigFontButton"><!--命名为大字体--> <Setter Property="Control.FontFamily" Value=" Times new Roman"></Setter> <Setter Property="Control.FontSize" Value="32"></Setter> <Style.Triggers><!--触发器集合--> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="Control.IsFocused" Value="True"></Condition> <Condition Property="Control.IsMouseOver" Value="True"></Condition> </MultiTrigger.Conditions> <MultiTrigger.Setters> <Setter Property="Control.Foreground" Value="Red"></Setter> </MultiTrigger.Setters> </MultiTrigger> </Style.Triggers> </Style> </Window.Resources> <StackPanel VerticalAlignment="Center"> <Button Content="Button" Style="{StaticResource BigFontButton}"/> <TextBox Margin="10" Text="必须满足两个条件才能触发触发器"/> </StackPanel> </Window>
事件触发器
<Window x:Class="WpfApp.Window2" 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:WpfApp" mc:Ignorable="d" Title="Window2" Height="450" Width="800"> <Window.Resources> <Style x:Key="EventTrriger"> <Style.Setters> <Setter Property="Control.FontSize" Value="red"></Setter> <Setter Property="Control.FontFamily" Value="Yellow"></Setter> </Style.Setters> <Style.Triggers> <EventTrigger RoutedEvent="Mouse.MouseEnter"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="FontSize" To="48"></DoubleAnimation> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger><!--路由事件--> </Style.Triggers> </Style> </Window.Resources> <StackPanel> <Button Name="bt_1" Content="事件触发器" Style="{StaticResource EventTrriger}"/> </StackPanel> </Window>
喝的不是酒,是心痛;抽的不是烟,是忧愁;