Monday, August 22, 2011

Triggers in WPF


Triggers are collection of setters and execute when condition met. Triggers are part of Style and can be defined inside style. For more information about Style you can go through my article on Style. Five types of Triggers are available in WPF
  • Trigger (Property Trigger)
  • MultiTrigger (Multiple Property Trigger)
  • DataTrigger
  • MultiDataTrigger
  • EventTrigger

Trigger

These triggers are simple triggers and can set any dependency property. Trigger defines property and value. If property contains specified value then the setter action defined inside trigger will execute.

<Window.Resources>
<Style TargetType="Button">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="FontSize" Value="20" />
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="Foreground" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
    <Button Content="Click Me!" Height="40" Width="150" Margin="5" />
</StackPanel>








As per above code, When IsMouseOver property value is true the defined setter action inside trigger will execute.

MultiTrigger

This is similar to Trigger but combines multiple conditions and all the condition must met to execute setter collection defined in multitrigger.

<Window.Resources>
<Style TargetType="Button">
    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsDefault" Value="True" />
                <Condition Property="IsMouseOver" Value="True" />
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
                <Setter Property="FontSize" Value="20" />
                <Setter Property="FontWeight" Value="Bold" />
                <Setter Property="Foreground" Value="Red" />
            </MultiTrigger.Setters>
        </MultiTrigger>
    </Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
    <Button Content="Click Me!" IsDefault="True" 
            Height="40" Width="150" Margin="5" />
</StackPanel>








As per above code, When IsDefault and IsMouseOver both properties value set to true the defined setter action inside multitrigger will execute.


DataTrigger

Data triggers are similar to normal trigger except that they can be triggered by any property rather than just only dependency property. In data trigger we need to specify binding instead of property name.

<Window.Resources>
<Style TargetType="Button">
    <Style.Triggers>
        <DataTrigger  Binding="{Binding
                      RelativeSource={RelativeSource Self},
                      Path=Content}" Value="Click Me!">
            <DataTrigger.Setters>
                <Setter  Property="FontSize" Value="20" />
            </DataTrigger.Setters>
        </DataTrigger>
    </Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
    <Button Content="Click Me!"
            Height="40" Width="150" Margin="5" />
</StackPanel>









As per above code, Data trigger checks value of Content property of Button and if value matches then it will execute setter block of data trigger. As per above example Button content is 'Click Me!' which matches the value mentioned in data trigger so it will execute setter block. In data trigger binding you can use any property of control or entity.

MultiDataTrigger

This is similar to DataTrigger but combines multiple conditions and all the condition must met to execute setter collection defined in MultiDataTrigger.

<Window.Resources>
<Style TargetType="Button">
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition  Binding="{Binding
                  RelativeSource={RelativeSource Self},
                  Path=Content}" Value="Click Me!" />
                <Condition  Binding="{Binding
                  RelativeSource={RelativeSource Self},
                  Path=IsDefault}" Value="True" />
            </MultiDataTrigger.Conditions>
            <MultiDataTrigger.Setters>
                <Setter  Property="FontSize" Value="20" />
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<Button Content="Click Me!" IsDefault="True"
        Height="40" Width="150" Margin="5" />
</StackPanel>








As per above code, When Content property and IsDefault property value match with the value mentioned in data trigger then the setter collection of MultiDataTrigger will execute. Both conditions must match.

EventTriggers

Event triggers are little different than other triggers. It depends on Routed Event. When certain routed event fires the event triggers action will execute. Instead of setter collection EventTrigger have Action collection. Inside action you can specify Animation. Inside action storyboard can be specified. I will explain about Animation and Storyboard in detail in my later post.

<Window.Resources>
<Style TargetType="Button">
<Style.Triggers>
    <EventTrigger RoutedEvent="MouseEnter">
        <EventTrigger.Actions>
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation
                        Duration="0:0:0.5"
                        Storyboard.TargetProperty="Width"
                        To="200"  />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger.Actions>
    </EventTrigger>
    <EventTrigger RoutedEvent="MouseLeave">
        <EventTrigger.Actions>
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation
                        Duration="0:0:0.5"
                        Storyboard.TargetProperty="Width"
                        To="150" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger.Actions>
    </EventTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<Button Content="Click Me!"
Height="40" Width="150" Margin="5" />
</StackPanel>


On Mouse Enter




On Mouse Leave











As per above code, MouseEnter and MouseLeave event trigger is specified. So whenever these events fires the EventTriggerAction will get execute. In above example Button width animation is specified. So when mouse enters inside button area the width of button will set to 200 and on mouse leave set back to 150.


See also

3 comments: