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">



MainWindow.xaml

<Window x:Class="PropertyTrigger.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="Property_Trigger" TargetType="{x:Type Button}">
            <Setter Property="Opacity" Value="1.0"></Setter>
            <Style.Triggers>
                <Trigger Property="IsFocused" Value="true">
                    <Setter Property="Opacity" Value="0.5">
                    </Setter>
                </Trigger>
            </Style.Triggers>

        </Style>

    </Window.Resources>
    <Grid>
        <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" />
    </Grid>
</Window>



MainWindow.xaml.cs

using System.Windows;

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

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


Output:













About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net 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

0 comments:

adsense

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