Skip to main content

WPF Property Trigger

Introduction

This article demonstrates how to use Property trigger with WPF UI controls and briefs little about Property Trigger. In this article we have covered example of both XAML and Behind code.

Getting Started

A Property Trigger gets executed when property of WPF UI control is changed. For example TextBlockForegroundcolor can be changes with the help of property trigger when IsMouseOverproperty is gets changed.




Triggers are declared with style and the setter property is used with in the trigger to gets change the target Control. The syntax of declaring trigger in XAML is like below.

 <Style TargetType="typ_of_control" x:Key="key_name_of_style">  
      <Style.Triggers>  
           <Trigger Property="Name_Of_Property" Value="value_Of_Property">  
                <Setter Property="PropertyName_Of_Target_Element" Value="Value_To_Be_Change"></Setter>  
           </Trigger>  
      </Style.Triggers>  
 </Style>  

The Property Named 'Property' in trigger accepts the name of property of bound element (whose visual is going to change) that means the trigger will check the value of that property. The Value property of the trigger accepts the value, based whom trigger will execute that means trigger will be fired when 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.

XAML Code Example

Declaration
  <Style TargetType="TextBlock" x:Key="tbktrigger">  
       <Style.Triggers>  
         <Trigger Property="IsMouseOver" Value="true">  
           <Setter Property="Foreground" Value="Red"></Setter>  
         </Trigger>  
       </Style.Triggers>  
     </Style>  
Use of Trigger
  <TextBlock Style="{StaticResource tbktrigger}" x:Name="textBlock" Text="Property Trigger Example"/>  

Behind Code Example

This example demonstrates how to declare trigger using behind code in C# and use that style into controls. it also gives example how to use style for trigger when you declared in xaml.
  Style style = new Style();  
       style.TargetType = typeof(TextBlock);  
       Trigger trigger = new Trigger();  
       trigger.Property = TextBlock.IsMouseOverProperty;  
       trigger.Value = true;  
       Setter setter = new Setter();  
       setter.Property = TextBlock.ForegroundProperty;  
       setter.Value = Brushes.Red;  
       trigger.Setters.Add(setter);  
       style.Triggers.Add(trigger);  
       this.textBlock.Style =style;  
When Style Declared in XAML and Use in Behind Code
  this.textBlock.Style = (Style)Application.Current.Resources["ListViewItemTextBlockStyle"];  

Related Articles

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

Summary

In this article we demonstrate how to declare property trigger and how to use that in both xaml and behind code. Hope this demonstrator will make you helpful.

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