Sunday, December 28, 2014

Property Trigger in WPF

11:14 PM - By azeheruddin khan 0

Explanation: - A trigger initiates or starts an action as a response to a routed event or as a response to changes made in values assigned to the properties of a control.
A property trigger enables styles and control templates to change the value of a control's property in response to change made in the value of another property of that control. For ex: - as shown in the above given program, the value of the property: Opacity (brightness characteristic) of a button decreases when the Button gets focus by clicking it (i.e. there is a change in focus property of the button).

Steps to define property trigger: -

1) Define a Trigger element and set the property of that element
        <Style x:Key="Property_Trigger" TargetType="{x:Type Button}">
        <Setter Property="Opacity" Value="1.0"></Setter>

2) Specify the Property which acts like a trigger.
         <Trigger Property="IsFocused" Value="true">

3) Define the behavior of the trigger.
       <Setter Property="Opacity" Value="0.5">


<Window x:Class="PropertyTrigger.MainWindow"
        Title="MainWindow" Height="350" Width="525">
        <Style x:Key="Property_Trigger" TargetType="{x:Type Button}">
            <Setter Property="Opacity" Value="1.0"></Setter>
                <Trigger Property="IsFocused" Value="true">
                    <Setter Property="Opacity" Value="0.5">


        <Button Content="FOCUS" Height="59" Style="{StaticResource Property_Trigger}" GotFocus="focus_GotFocus" HorizontalAlignment="Left" Margin="171,102,0,0" Name="focus" VerticalAlignment="Top" Width="142" />


using System.Windows;

namespace PropertyTrigger
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        public MainWindow()

          private void focus_GotFocus(object sender, RoutedEventArgs e)
            MessageBox.Show("the brightness of the button decreases when it gets focused by clicking it");


About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, and ms sql.My work comprise of medium and enterprise level projects using and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post



© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top