Kailash's Blogs

WPF Data Trigger

Kailash Chandra Behera | Saturday, July 09, 2016
Kailash's Blogs


This article describes WPF Data Trigger how to declare and use of it with example in both XAML and Behind Code using c#,

Getting Started

WPF Data Trigger gets executed when bound data gets changes and meets the condition. For example lets say there are two TextBox textBox and textBox1. The Text property of textBox1 is bound with Text property of textBox. When the textBox text is red then the background color of textBox1 should be red and when black then background color should be black. This can be achieved through DataTrigger.

Syntax in XAML

  <Style TargetType="Control_Type">  
             <DataTrigger Binding="{}" Value="red">  
               <Setter Property="property_name_towhome_change" Value="what to change in property"></Setter>  

The Property Named 'Binding' in trigger accepts the name of the property of bound element or class whose value trigger will check. The Value property of the trigger accepts the value, based whose trigger will execute which means the trigger will be fired when the property value of bound element matches the provided value.

The setters in the trigger are the collection of setter objects, the setter object is actually changing the UI element vitality. The property named 'Property' accepts the value that is going to be changed based on the Value property of setter.

Example in XAML

 <Style TargetType="TextBox" x:Key="dtrigger">  
         <DataTrigger Binding="{Binding ElementName=textBox, Path=Text}" Value="red">  
           <Setter Property="Background" Value="Red"></Setter>  
         <DataTrigger Binding="{Binding ElementName=textBox, Path=Text}" Value="blue">  
           <Setter Property="Background" Value="Blue"></Setter>  
         <DataTrigger Binding="{Binding ElementName=textBox, Path=Text}" Value="black">  
           <Setter Property="Background" Value="Black"></Setter>  

Declaration of WPF Data Trigger using XAML

 <TextBox x:Name="textBox" />  
     <TextBox Style="{StaticResource dtrigger}" x:Name="textBox1" >  

Use of WPF Data Trigger using CSharp

Behind Code Example

       Style style = new Style();  
       style.TargetType = typeof(TextBox);  
       Binding binding = new Binding();  
       binding.ElementName = "textBox";  
       binding.Path = new PropertyPath("Text");  
       DataTrigger redtrigger = new DataTrigger();  
       redtrigger.Binding = binding;  
       redtrigger.Value = "red";  
       Setter redsetter = new Setter();  
       redsetter.Property = TextBlock.BackgroundProperty;  
       redsetter.Value = Brushes.Red;  
       DataTrigger bluetrigger = new DataTrigger();  
       bluetrigger.Binding = binding;  
       bluetrigger.Value = "blue";  
       Setter bluesetter = new Setter();  
       bluesetter.Property = TextBlock.BackgroundProperty;  
       bluesetter.Value = Brushes.Blue;  
       DataTrigger blacktrigger = new DataTrigger();  
       blacktrigger.Binding = binding;  
       blacktrigger.Value = "black";  
       Setter blacksetter = new Setter();  
       blacksetter.Property = TextBlock.BackgroundProperty;  
       blacksetter.Value = Brushes.Blue;  

Declaration of WPF Data Trigger using CSharp

 textBox1.Style = style;  
 this.textBox1.Style = (Style)Application.Current.Resources["ListViewItemTextBlockStyle"];  

Use of WPF Data Trigger using CSharp

Related Articles

  1. WPF Event Trigger
  2. WPF Property Trigger
  3. UpdateSourceTrigger
  4. WPF Triggers
  5. WPF Converters


In this article we have demonstrates how to declare triggers in style using both XAML and behind code. Hope this article may helpful to you.