Change Value of Properties using Triggers in WPF Application

Updated on     Kisan Patel

This post will show you how to change the value of properties using triggers in WPF application?

You can change the look and feel of controls in WPF application using the PropertyTriggers trigger.

You can assign a trigger by using the IsMouseOver or IsKeyboardFocused property.

MainWindow.xaml

<Window x:Class="WPFStackPanelDemo.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">
      <Grid>
         <Grid.Resources>
            <Style x:Key="ChangeStyle" TargetType="{x:Type TextBox}">
               <Setter Property="Background" Value="Red"/>
               <Setter Property="FontSize" Value="12"/>
               <Style.Triggers>
                   <Trigger Property="IsMouseOver" Value="True">
                       <Setter Property="Background" Value="Blue"/>
                       <Setter Property="FontSize" Value="20"/>
                   </Trigger>
               </Style.Triggers>
            </Style>
          </Grid.Resources>
          <TextBox Width="200" Height="40" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0, 10, 0, 0"
Style="{StaticResource ChangeStyle}" />
          <TextBox Width="250" Height="40" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0, 100, 0, 0"
Style="{StaticResource ChangeStyle}" />
      </Grid>
</Window>

When you run the application, you can see below output:

Triggers-demo-wpf


WPF

Leave a Reply