in Education by
What is Trigger and how many types of triggers in WPF?

1 Answer

0 votes
by
A Trigger is typically used in a Style or Control Template. It triggers on properties of whatever is being templated, and sets other properties of the control (or of specific template elements). For example, you would use a Trigger on IsMouseOver to respond to the mouse being over the control, and the setters might update a brush to show a "hot" effect.
 
Why to use Trigger
 
Triggers are used in styles to perform actions on a change of any property value or event fires. Triggers create visual effects on controls. By using Triggers we can change the appearance of Framework Elements.
 
There are five types of triggers supported by WPF; they are:
  1. Property Trigger
  2. Data Trigger
  3. MultiTrigger
  4. MultiDataTrigger
  5. Event Trigger
Example 1: 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.
  1. private void Rectangle_MouseMove_1(object sender, MouseEventArgs e)   
  2. {   
  3.    this.rctback.Fill = Brushes.Red;   
  4. }   
  5. private void Rectangle_MouseLeave(object sender, MouseEventArgs e)   
  6. {   
  7.    this.rctback.Fill = Brushes.Green;   
  8. }   

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.

Related questions

0 votes
    What is WPF Dependency Property and how can we use?...
asked Apr 9, 2021 in Education by JackTerrance
0 votes
    What is the Data Binding concept and How Binding works in WPF?...
asked Apr 8, 2021 in Education by JackTerrance
0 votes
0 votes
    What is Command Design Pattern and ICommand in WPF?...
asked Apr 8, 2021 in Education by JackTerrance
0 votes
    What is the Tab Control in WPF?...
asked Apr 9, 2021 in Education by JackTerrance
0 votes
0 votes
0 votes
    What are the various layout panels in WPF ?...
asked Apr 9, 2021 in Education by JackTerrance
...