Skip to main content

WPF Triggers

Introduction

Triggers in WPF is same as like triggers in SQL, This article describes about WPF trigger, different types of triggers are available in WPF.

Getting Started

Triggers are a very important feature of WPF that helps to change the visual effect of a framework element or control. Triggers are typically used in styles or ControlTemplate to change the visual effect of a WPF element when a property or data is changed or an event is fired. For changing the visual effect of a WPF element, triggers reduce the code in code behind.

For example, let's say you have a rectangle control. You want to change the background color of that control when the mouse hovers over the rectangle control and revert back when mouse leaves. For this you need to code in the mouse hover event and mouse leave event of the rectangle control in the backend class for changing the color of rectangle as in the following code.

 private void Rectangle_MouseMove_1(object sender, MouseEventArgs e)   
 {   
   this.rctback.Fill = Brushes.Red;   
 }   
 private void Rectangle_MouseLeave(object sender, MouseEventArgs e)   
 {   
   this.rctback.Fill = Brushes.Green;   
 }   

In the preceding code you have filled the background color of the Rectangle control by writing code in two different events, but a trigger helps to overcome this problem by reducing the code. See the following example.

 <Rectangle Name="rctback" HorizontalAlignment="Left" Height="100" Stroke="Black" VerticalAlignment="Top" Width="100" Margin="178,92,0,0" >   
       <Rectangle.Style>   
         <Style TargetType="Rectangle">   
           <Setter Property="Fill" Value="Green"></Setter>   
           <Style.Triggers>   
             <Trigger Property="IsMouseOver" Value="True">   
               <Setter Property="Fill" Value="Red" />   
             </Trigger>   
           </Style.Triggers>   
         </Style>   
       </Rectangle.Style>   
 </Rectangle>   

Types of Trigger

Mainly there are three types of trigger in WPF. The triggers are like this

  1. Property Trigger :-

    This is the default trigger in WPF. This trigger gets fired when property of target element or own control is changed.

  2. Data Trigger :- This trigger is fires when the data bound with the control is changed and matches the condition.
  3. Event Trigger:- An Event trigger is fired when an event fired by UI Element.

Related Articles

  1. WPF Property Trigger
  2. WPF Data Trigger
  3. WPF Event Trigger

Summary

We have discussed about Trigger of WPF in this article, hope this article may helpful to you.

Thanks

Comments

Popular posts from this blog

Generate QR Code in WPF

Introduction In my previous two blogs, we have discussed how to display generate and display various barcodes on the web page. In this blog, we are going to demonstrate how to Generate QR Code in the WPF application. Getting Started Here in the demonstration Generate QR Code in WPF , will generate QR code using third party library as there is not inbuilt library provided by Microsoft to generate QR code and will display in WPF image control, In the below, we will see the steps to display QR Code. As I mentioned in the above paragraph that there is no inbuild library provided by Microsoft to generate QR code, I have taken the help of the ZXing library which is a third party free library and available Nuget. This library provides various options to generate barcodes and QR Code from the user-friendly text. Generate QR Code in WPF Here are the steps to generate QR code and let's follow the steps to complete demonstrations. Demonstration:- Generate QR Code Open visual stud

Creating Application in Prism

Introduction This article explains an illustration of creating a windows application in Prism Library(WPF Prism). The solution includes recommended practices and techniques and is the basis for the procedures in Prism. This illustration created in the Visual Studio 2012, It can also developed in the visual studio 2008 and 2010, because wpf supports from .net framework 3.5 to latest version. Microsoft.Practices.Prism.dll. This assembly contains the implementation of the Prism Library core components such as modularity, logging services, communication services, and definitions for several core interfaces. It also contains the implementation of Prism Library components that target WPF applications, including commands, regions, and events. Microsoft.Practices.Prism.UnityExtensions.dll. This assembly contains base and utility classes you can reuse in applications built with the Prism Library that consume the Unity Application Block. For example, it contains a bootstrapper base class

WPF Binding

Introduction This blog describes WPF Binding and the measure elements exist with binding with XAML example in XAML and code-behind. Getting Started Binding helps in WPF to flow data from one object to another object, the object which fetches data is called source and the object which receives the data is called target. The Object a be a UI control or object of a class that means in binding you can bind a property of a class and property of another control to WPF UI controls as well. XAML Example <TextBox x:Name="sourceText" Grid.Row="0" /> <Button Grid.Row="1" Content="{Binding ElementName=sourceText, Path=Text}"/> Code Example Button btn=new Button(); Binding binding = new Binding("Text"); binding.Source = sourceText; btn.SetBinding(Button.ContentProperty, binding); In WPF binding has some measure properties or elements that we are using while developing application or projects, here we are going to d