WPF 多种触发器代码
view sourceprint?
001 <Window x:Class="多种触发器.Window1"
002 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
003 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
004 Title="Window1" Height="633" Width="669">
005 <Window.Resources>
006 <!-- 定义公共label资源-->
007 <Style x:Key="LabelStyle">
008 <Setter Property="Control.Background" Value="Wheat"></Setter>
009 <Setter Property="Control.BorderBrush" Value="Black"></Setter>
010 <Setter Property="Control.BorderThickness" Value="1"></Setter>
011 </Style>
012 <!--BaseOn 继承上面的资源,一个简单的事件触发器。-->
013 <Style x:Key="Triggers" BasedOn="{StaticResource LabelStyle}">
014 <Style.Triggers>
015 <Trigger Property="Control.IsMouseOver" Value="True">
016 <Setter Property="Control.Background" Value="Red"></Setter>
017 </Trigger>
018 </Style.Triggers>
019 </Style>
020 <!---->
021 <Style x:Key="MultiTrigger" BasedOn="{StaticResource LabelStyle}">
022 <Style.Triggers>
023 <MultiTrigger>
024 <MultiTrigger.Conditions>
025 <Condition Property="Control.IsMouseOver" Value="True"></Condition>
026 <Condition Property="Control.IsFocused" Value="False"></Condition>
027 </MultiTrigger.Conditions>
028 <MultiTrigger.Setters>
029 <Setter Property="Control.Background" Value="Green"></Setter>
030 </MultiTrigger.Setters>
031 </MultiTrigger>
032 </Style.Triggers>
033 </Style>
034 <!--EventTrigger多条件触发器 -->
035 <Style x:Key="EventTrigger" BasedOn="{StaticResource LabelStyle}">
036 <Style.Triggers>
037 <EventTrigger RoutedEvent="Mouse.MouseEnter">
038 <EventTrigger.Actions>
039 <BeginStoryboard Name="ExpendWidth">
040 <Storyboard>
041 <DoubleAnimation Duration="0:0:5" Storyboard.TargetProperty="Width" To="300"></DoubleAnimation>
042 <DoubleAnimation Duration="0:0:5" Storyboard.TargetProperty="Height" To="200"></DoubleAnimation>
043 <DoubleAnimation Duration="0:0:5" Storyboard.TargetProperty="FontSize" To="30" ></DoubleAnimation>
044 </Storyboard>
045 </BeginStoryboard>
046 </EventTrigger.Actions>
047 </EventTrigger>
048 <EventTrigger RoutedEvent="Mouse.MouseLeave">
049 <EventTrigger.Actions>
050 <BeginStoryboard>
051 <Storyboard>
052 <DoubleAnimation Duration="0:0:0.1" Storyboard.TargetProperty="FontSize"></DoubleAnimation>
053 <DoubleAnimation Duration="0:0:0.1" Storyboard.TargetProperty="Width"></DoubleAnimation>
054 <DoubleAnimation Duration="0:0:0.1" Storyboard.TargetProperty="Height"></DoubleAnimation>
055 </Storyboard>
056 </BeginStoryboard>
057 </EventTrigger.Actions>
058 </EventTrigger>
059 </Style.Triggers>
060 </Style>
061 </Window.Resources>
062
063 <StackPanel Orientation="Vertical">
064 <Label Height="28" Name="label1" VerticalAlignment="Top" Width="79" Style="{StaticResource Triggers}" >简单触发器</Label>
065 <Label Height="28" Name="label2" VerticalAlignment="Top" Width="90" Margin="10,10" Style="{StaticResource MultiTrigger}" Foreground="Red" >多条件触发器</Label>
066 <Label Height="40" Name="label4" Margin="0,0,0,10" Width="120" Style="{StaticResource EventTrigger}" VerticalContentAlignment="Center" HorizontalContentAlignment="Center">事件触发器</Label>
067
068 <Label Height="40" Name="label3" Width="120" Style="{StaticResource LabelStyle}" VerticalContentAlignment="Center" HorizontalContentAlignment="Center">事件触发器</Label>
069
070 <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="100">
071 <Button Height="23" Name="button1" Width="75">stop</Button>
072 <Button Height="23" Name="button2" Width="75">Resume</Button>
073 <Button Height="23" Name="button3" Width="75">Seek</Button>
074 <Button Height="23" Name="button4" Width="75">Skip</Button>
075 <Button Height="23" Name="button5" Width="75">SetSpeed</Button>
076 </StackPanel>
077
078 <StackPanel.Triggers>
079 <EventTrigger RoutedEvent="Mouse.MouseEnter" SourceName="label3">
080 <EventTrigger.Actions>
081 <BeginStoryboard Name="ExpendWidth">
082 <Storyboard>
083 <DoubleAnimation Duration="0:0:10" Storyboard.TargetProperty="Width" To="400" Storyboard.TargetName="label3" ></DoubleAnimation>
084 <DoubleAnimation Duration="0:0:10" Storyboard.TargetProperty="Height" To="300" Storyboard.TargetName="label3" AutoReverse="True"></DoubleAnimation>
085 </Storyboard>
086 </BeginStoryboard>
087 </EventTrigger.Actions>
088 </EventTrigger>
089 <EventTrigger RoutedEvent="Mouse.MouseLeave" SourceName="label3">
090 <EventTrigger.Actions>
091 <BeginStoryboard>
092 <Storyboard>
093 <DoubleAnimation Duration="0:0:0.1" Storyboard.TargetProperty="Width" Storyboard.TargetName="label3"></DoubleAnimation>
094 </Storyboard>
095 </BeginStoryboard>
096 </EventTrigger.Actions>
097 </EventTrigger>
098 <EventTrigger RoutedEvent="Button.Click" SourceName="button1">
099 <PauseStoryboard BeginStoryboardName="ExpendWidth"></PauseStoryboard>
100 </EventTrigger>
101 <EventTrigger RoutedEvent="Button.Click" SourceName="button2">
102 <ResumeStoryboard BeginStoryboardName="ExpendWidth"></ResumeStoryboard>
103 </EventTrigger>
104 <EventTrigger RoutedEvent="Button.Click" SourceName="button3">
105 <SeekStoryboard BeginStoryboardName="ExpendWidth" Offset="0:0:0" Origin="BeginTime"></SeekStoryboard>
106 </EventTrigger>
107 <EventTrigger RoutedEvent="Button.Click" SourceName="button4">
108 <SkipStoryboardToFill BeginStoryboardName="ExpendWidth" />
109 </EventTrigger>
110 <EventTrigger RoutedEvent="Button.Click" SourceName="button5">
111 <SetStoryboardSpeedRatio SpeedRatio="3" BeginStoryboardName="ExpendWidth" />
112 </EventTrigger>
113 </StackPanel.Triggers>
114 </StackPanel>
115 </Window>