HI WELCOME TO SIRIS

Different Types of Triggers in WPF

Triggers are used to change the style of a control when control’s property value changes or event fires. Triggers create visual effects on controls. By using triggers you can also change the appearance of framework elements.

Types of Triggers in WPF

  1. Property Trigger

    This trigger gets active when UIElements property value changes. The below Trigger is created on Button. It will set Opacity to 0.5 when Buttons IsPressed property change.
    1. <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
    2. <Style.Triggers>
    3. <Trigger Property="IsPressed" Value="True">
    4. <Setter Property="Opacity" Value="0.5" />
    5. </Trigger>
    6. <Trigger Property="IsEnabled" Value="False">
    7. <Setter Property="Foreground" Value="Red" />
    8. </Trigger>
    9. </Style.Triggers>
    10. </Style>
  2. Event Trigger

    This trigger gets active when RoutedEvent of FrameworkElement raise. Event Trigger is generally used to perform some animation on control like as colorAnimation, doubleAnumation using KeyFrame etc.
    The below trigger is used to animate/change the color property (SolidColorBrush, LinearGradientBrush ) of the UIElement at defined time duration.
    1. <Border Name="border1" Width="100" Height="30"
    2. BorderBrush="Black" BorderThickness="1">
    3. <Border.Background>
    4. <SolidColorBrush x:Name="MyBorder" Color="LightBlue" />
    5. </Border.Background>
    6. <Border.Triggers>
    7. <EventTrigger RoutedEvent="Mouse.MouseEnter">
    8. <BeginStoryboard>
    9. <Storyboard>
    10. <ColorAnimation Duration="0:0:1"
    11. Storyboard.TargetName="MyBorder"
    12. Storyboard.TargetProperty="Color"
    13. To="Gray" />
    14. </Storyboard>
    15. </BeginStoryboard>
    16. </EventTrigger>
    17. </Border.Triggers&
    18. gt;
    19. </Border>
  3. Data Trigger

    This trigger gets active when Binding Data matches specified condition. Below DataTrigger is created on Picture property of binding data. Setter objects of the DataTrigger describes property values to apply when binding data match the condition. Picture is Byte[] property of the class. If Picture is null then DataTrigger applies on image to set image source to noImage.png, otherwise it will set image source from picture.
    Same as if picture is null, applies TextBlock value to "No Image Availalbe" and foreground color to red otherwise sets default value from data.
    1. <DataTemplate>
    2. <Grid Margin="0 5 0 0">
    3. <Grid.RowDefinitions>
    4. <RowDefinition Height="Auto" />
    5. <RowDefinition Height="Auto" />
    6. </Grid.RowDefinitions>
    7. <Image x:Name="viewImage"
    8. Grid.Row="0" Width="100"
    9. Height="60" HorizontalAlignment="Center"
    10. Source="{Binding Picture}" Stretch="Fill" />
    11. <TextBlock x:Name="viewText"
    12. Grid.Row="1" Margin="0 5 0 0"
    13. HorizontalAlignment="Center"
    14. FontWeight="Black" Foreground="Green"
    15. Text="{Binding Title}" />
    16. </Grid>
    17. <DataTemplate.Triggers>
    18. <DataTrigger Binding="{Binding Path=Picture}" Value="{x:Null}">
    19. <Setter TargetName="viewImage" Property="Source" Value="/Images/noImage.png" />
    20. <Setter TargetName="viewText" Property="Text" Value="No Image Available" />
    21. <Setter TargetName="viewText" Property="Foreground" Value="Red" />
    22. </DataTrigger>
    23. </DataTemplate.Triggers>
    24. </DataTemplate>
    What do you think?
    I hope you will enjoy the tips while programming with WPF. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.