WPF Data Trigger

Introduction

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">  
           <Style.Triggers>  
             <DataTrigger Binding="{}" Value="red">  
               <Setter Property="property_name_towhome_change" Value="what to change in property"></Setter>  
             </DataTrigger>  
           </Style.Triggers>  
 </Style>  

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">  
       <Style.Triggers>  
         <DataTrigger Binding="{Binding ElementName=textBox, Path=Text}" Value="red">  
           <Setter Property="Background" Value="Red"></Setter>  
         </DataTrigger>  
         <DataTrigger Binding="{Binding ElementName=textBox, Path=Text}" Value="blue">  
           <Setter Property="Background" Value="Blue"></Setter>  
         </DataTrigger>  
         <DataTrigger Binding="{Binding ElementName=textBox, Path=Text}" Value="black">  
           <Setter Property="Background" Value="Black"></Setter>  
         </DataTrigger>  
       </Style.Triggers>  
     </Style>  

Declaration of WPF Data Trigger using XAML

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

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;  
       redtrigger.Setters.Add(redsetter);  
       DataTrigger bluetrigger = new DataTrigger();  
       bluetrigger.Binding = binding;  
       bluetrigger.Value = "blue";  
       Setter bluesetter = new Setter();  
       bluesetter.Property = TextBlock.BackgroundProperty;  
       bluesetter.Value = Brushes.Blue;  
       redtrigger.Setters.Add(bluesetter);  
       DataTrigger blacktrigger = new DataTrigger();  
       blacktrigger.Binding = binding;  
       blacktrigger.Value = "black";  
       Setter blacksetter = new Setter();  
       blacksetter.Property = TextBlock.BackgroundProperty;  
       blacksetter.Value = Brushes.Blue;  
       redtrigger.Setters.Add(blacksetter);  
       style.Triggers.Add(redtrigger);  
       style.Triggers.Add(bluetrigger);  
       style.Triggers.Add(bluetrigger);  

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

Summary

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.

Thanks

Kailash Chandra Behera

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

Previous Post Next Post

نموذج الاتصال