WPF

WPF Trigger

Posted by Leo Yang on 2021-02-17

Trigger

DataTrigger

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<TextBlock x:Name="tbText"/>
<StackPanel Background="Black">
<StackPanel.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding={Binding ElementName=tbText, Path = Text} Value="Yes"/>
<Setter Property="Background" Value="Red"/>
</DataTrigger>
<DataTrigger Binding={Binding ElementName=tbText, Path = Text} Value="No"/>
<Setter Property="Background" Value="Blue"/>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
<Button Content="123"/>
</StackPanel>

EventTrigger

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<StackPanel Background="Black">
<StackPanel.Style>
<Style>
<Style.Triggers>
<EventTrigger RoutedEvent="Mouse.Enter">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Background.Color" From="Black" To="Transparent" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseLeave">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Background.Color" From="Transparent" To="Black" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
<Button Content="123"/>
</StackPanel>

Trigger

1
2
3
4
5
6
7
8
9
10
11
12
13
 <TextBlock Text="Hello, styled world!" FontSize="28" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Blue"></Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Red" />
<Setter Property="TextDecorations" Value="Underline" />
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>