WPF中的触发器

1. 简单触发器

<Window x:Class="项目10.MainWindow"
        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:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:项目10"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="500">
    <Window.Resources>
        <Style x:Key="BigFontButton">
            <Setter Property="Control.FontFamily" Value="Times New Roma"/>
            <Setter Property="Control.FontSize" Value="32"/>
            <Style.Triggers>
                <Trigger Property="Control.IsFocused" Value="True">
                    <Setter Property="Control.Foreground" Value="YellowGreen"></Setter>
                </Trigger>
                <Trigger Property="Button.IsPressed" Value="True">
                    <Setter Property="Control.Foreground" Value="BlueViolet"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <StackPanel Margin="5" x:Name="StackPanel1">
        <Button Style="{StaticResource BigFontButton}">Button</Button>
        <TextBox>safasf</TextBox>
    </StackPanel>
</Window>

2. 事件触发器

<Window x:Class="项目10.MainWindow"
        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:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:项目10"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="500">
    <Window.Resources>
        <Style x:Key="BigFontButton">
            <Setter Property="Control.FontFamily" Value="Times New Roma"/>
            <Setter Property="Control.FontSize" Value="18"/>
            <Style.Triggers>
                <EventTrigger RoutedEvent="Mouse.MouseEnter">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:0.8" Storyboard.TargetProperty="FontSize" To="48"></DoubleAnimation>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
                <EventTrigger RoutedEvent="Mouse.MouseLeave">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:0.8" Storyboard.TargetProperty="FontSize" To="15"></DoubleAnimation>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <StackPanel Margin="5" x:Name="StackPanel1">
        <Button Style="{StaticResource BigFontButton}">Button</Button>
        <TextBox>safasf</TextBox>
    </StackPanel>
</Window>