Skip to main content

WPF Data Trigger

Introduction

This article describes about 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 trigger is collection of setter object, the setter object is actually changing the UI element vitality. The property named 'Property' accepts the value that is going to be changes based on the Value property of setter.

Example in XAML

Declaration
 <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>  
Use
 <TextBox x:Name="textBox" />  
     <TextBox Style="{StaticResource dtrigger}" x:Name="textBox1" >  
     </TextBox>  

Behind Code Example

Declaration
       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);  
Use
 textBox1.Style = style;  
 this.textBox1.Style = (Style)Application.Current.Resources["ListViewItemTextBlockStyle"];  

Related Articles

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

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

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