What are Triggers?

Triggers are a medium by which we can trigger a behavior of the targeted control when it fulfills the specified conditions.

There are three types of Triggers.

Let us look at a scenario where we use all the triggers.

The View has three buttons, each of which uses all the above-mentioned types of Triggers.

WPF

We define the triggers in the XAML code, which looks as below.

  1. <Window x:Class="TriggersInWpf.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. mc:Ignorable="d"
  7. Title="Using Triggers in Wpf" Height="450" Width="800">
  8. <Window.Resources>
  9. <!--Define the property trigger-->
  10. <Style x:Key="PropertyTriggered" TargetType="Button">
  11. <Setter Property="Background" Value="Gainsboro"/>
  12. <Style.Triggers>
  13. <Trigger Property="IsMouseOver" Value="True">
  14. <Setter Property="Cursor" Value="Hand"/>
  15. </Trigger>
  16. </Style.Triggers>
  17. </Style>
  18. <!--Define the data trigger-->
  19. <Style x:Key="DataTriggered" TargetType="TextBlock">
  20. <Style.Triggers>
  21. <DataTrigger
  22. Binding="{Binding ElementName=BtnDataTrigger, Path=IsPressed}" Value="True">
  23. <Setter Property="Text" Value="DataTrigger has been triggered!"/>
  24. <Setter Property="FontWeight" Value="Bold"/>
  25. <Setter Property="VerticalAlignment" Value="Center"/>
  26. <Setter Property="HorizontalAlignment" Value="Center"/>
  27. <Setter Property="FontSize" Value="18"/>
  28. </DataTrigger>
  29. </Style.Triggers>
  30. </Style>
  31. </Window.Resources>
  32. <Grid>
  33. <Grid.RowDefinitions>
  34. <RowDefinition Height="50"/>
  35. <RowDefinition Height="50"/>
  36. <RowDefinition Height="50"/>
  37. <RowDefinition Height="50"/>
  38. <RowDefinition Height="50"/>
  39. <RowDefinition Height="Auto"/>
  40. </Grid.RowDefinitions>
  41. <Grid.ColumnDefinitions>
  42. <ColumnDefinition Width="300"/>
  43. <ColumnDefinition Width="100"/>
  44. <ColumnDefinition Width="Auto"/>
  45. </Grid.ColumnDefinitions>
  46. <Button x:Name="BtnKey"
  47. Content="HoverOverMeToCheckPropertyTrigger"
  48. Style="{StaticResource PropertyTriggered}"
  49. Grid.Row="0"
  50. Grid.Column="0" Width="300"/>
  51. <Button x:Name="BtnDataTrigger"
  52. Content="HowDataTriggerWorks? Press Me!"
  53. Style="{StaticResource PropertyTriggered}"
  54. Grid.Row="2"
  55. Grid.Column="0" Width="300"/>
  56. <TextBlock x:Name="TxtBlock"
  57. Style="{StaticResource DataTriggered}"
  58. Grid.Row="2"
  59. Grid.Column="2"/>
  60. <Button x:Name="BtnEventTrigger"
  61. Content="HowEventTriggerWorks? Press Me!"
  62. Style="{StaticResource PropertyTriggered}"
  63. Grid.Row="4"
  64. Grid.Column="0">
  65. <!--Define the event trigger-->
  66. <Button.Triggers>
  67. <EventTrigger RoutedEvent="Button.Click">
  68. <EventTrigger.Actions>
  69. <BeginStoryboard>
  70. <Storyboard>
  71. <DoubleAnimation
  72. Storyboard.TargetName="BtnEventTrigger"
  73. Storyboard.TargetProperty="Width"
  74. From="0" To="300"
  75. Duration="0:0:5">
  76. </DoubleAnimation>
  77. </Storyboard>
  78. </BeginStoryboard>
  79. </EventTrigger.Actions>
  80. </EventTrigger>
  81. </Button.Triggers>
  82. </Button>
  83. </Grid>
  84. </Window>

This is just a simple use of Triggers I have demonstrated. Triggers are very useful in our everyday coding but it must be STRESSED that the unnecessary/excessive use of triggers/converters in our code can lead to performance issues. So, just make sure that you really need it in your application.