Please enable Javascript to correctly display the contents on Dot Net Tricks!

Different Types of Triggers in WPF

  Author : Shailendra Chauhan
Posted On : 09 Oct 2013
Total Views : 27,173   
 

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.

    <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
     <Style.Triggers>
     <Trigger Property="IsPressed" Value="True">
     <Setter Property="Opacity" Value="0.5" />
     </Trigger>
     <Trigger Property="IsEnabled" Value="False">
     <Setter Property="Foreground" Value="Red" />
     </Trigger>
     </Style.Triggers>
    </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.

    <Border Name="border1" Width="100" Height="30"
     BorderBrush="Black" BorderThickness="1">
     <Border.Background>
     <SolidColorBrush x:Name="MyBorder" Color="LightBlue" />
     </Border.Background>
     <Border.Triggers>
     <EventTrigger RoutedEvent="Mouse.MouseEnter">
     <BeginStoryboard>
     <Storyboard>
     <ColorAnimation Duration="0:0:1"
     Storyboard.TargetName="MyBorder"
     Storyboard.TargetProperty="Color"
     To="Gray" />
     </Storyboard>
     </BeginStoryboard>
     </EventTrigger>
     </Border.Triggers&
     gt;
    </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.

    <DataTemplate>
     <Grid Margin="0 5 0 0">
     <Grid.RowDefinitions>
     <RowDefinition Height="Auto" />
     <RowDefinition Height="Auto" />
     </Grid.RowDefinitions>
     <Image x:Name="viewImage"
     Grid.Row="0" Width="100"
     Height="60" HorizontalAlignment="Center"
     Source="{Binding Picture}" Stretch="Fill" />
     <TextBlock x:Name="viewText"
     Grid.Row="1" Margin="0 5 0 0"
     HorizontalAlignment="Center"
     FontWeight="Black" Foreground="Green"
     Text="{Binding Title}" />
     </Grid>
     <DataTemplate.Triggers>
     <DataTrigger Binding="{Binding Path=Picture}" Value="{x:Null}">
     <Setter TargetName="viewImage" Property="Source" Value="/Images/noImage.png" />
     <Setter TargetName="viewText" Property="Text" Value="No Image Available" />
     <Setter TargetName="viewText" Property="Foreground" Value="Red" />
     </DataTrigger>
     </DataTemplate.Triggers>
    </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.



Free Interview Books
 
COMMENTS (0)
6 JUN
Hadoop Development cum Administration (Online)

7:00 AM-9:00AM IST

Know More
30 MAY
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

Know More
30 MAY
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

28 MAY
ASP.NET Core with Angular4 Development (Classroom)

10:30 AM-11:30 AM Weekend

27 MAY
ASP.NET MVC with Angular4 Development (Classroom)

4:00 PM-5:30 PM Weekend

20 MAY
Angular2 and Angular4 Development (Online)

08:00 AM-10:00 AM IST(+5.30 GMT) Weekend

20 MAY
NodeJS with Angular4 Development (Classroom)

5:00 PM-6:30 PM Weekend

8 MAY
ASP.NET MVC with Angular4 Development (Online)

07:00 AM-09:00 AM IST(+5:30 GMT) M/W/F

2 MAY
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST( TUS, THR)

29 APR
ASP.NET MVC with Angular2 Development (Classroom)

9:30 AM-11:00 AM

22 APR
MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM Weekend

10 APR
Master Class ASP.NET MVC 5 with Angular2 Plus Angular4 Development (Online)

09:00 PM - 11:00 PM IST (M/W/F)

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+