WPF Trigger Properties

I am new to WPF and now I am working with triggers . I have a question regarding a simple trigger. With a simple trigger, I mean one that monitors the change in the dependency property and uses setter to change the style.

Example:

 <Style.Triggers> <Trigger Property="Control.IsFocused" Value ="True"> <Setter Property=" Control.Foreground" Value =" DarkRed" /> </Trigger> </Style.Triggers> 

All the examples I saw used the following trigger properties :

  • <Trigger Property="Control.IsFocused" Value ="True">
  • <Trigger Property="Control.IsMouseOver" Value ="True">
  • <Trigger Property="Button.IsPressed" Value ="True">

Question : Are these trigger properties available? If not, then what are the others?

I searched the Internet, but to no avail. Maybe someone can shed some light on this.

+6
source share
3 answers

These are not the only properties you can use in your Triggers , however they are common examples because they are easy to understand and easy to demonstrate.

In truth, you can watch Trigger on a Trigger DependencyProperty , but since it "fires" when the value changes (and matches the Value that you say to keep track of it), it only makes sense to change at run time, often from - due to user action (for example, focus, mouse, clicking, etc.). Only certain values ​​of the actual DependencyProperties replacement in these circumstances, so not all of them make sense to use in Triggers .

Microsoft has added several standard DependencyProperties to the standard controls so you can easily create triggers based on changes. However, you can also create your own controls using your own DependencyProperties and have triggers that respond when custom DependencyProperties changes.

Keep in mind PropertyTriggers is just one Trigger flavor in WPF. There are also EventTriggers and DataTriggers and MultiTriggers . These other triggers fire depending on events or data changes or in the case of MultiTriggers values ​​of several properties (or data).

Is there anything specific you are trying to do with Triggers ? This other answer gives a good explanation of what each type of trigger does.

+11
source

There are several types of triggers in WPF, but the two most commonly used are regular Triggers and DataTriggers

Both types of triggers will observe the value, and when it changes according to the specified Value , then they will apply your style settings.

Regular triggers can be used for any property of object dependencies. This includes properties such as Text , Visibility , Background , etc. In addition to the more frequently set properties you specified: IsFocused , IsMouseOver and IsPressed .

Note that on the page or "{Binding ElementName=MyTextBox, Path=IsChecked}" . You can even use Converters with DataTriggers, for example

 <DataTrigger Binding="{Binding SomeInt, Converter={StaticResource IsGreaterThanZero}}" Value="True"> 
+6
source

Use this code for a better wpf trigger experience.

 <Window x:Class="DataBinding.Trigger2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Trigger2" Height="500" Width="500"> <Window.Resources> <Style TargetType="Button"> <Style.Setters> <Setter Property="FontFamily" Value="Tahoma"></Setter> <Setter Property="FontSize" Value="15"></Setter> <Setter Property="FontWeight" Value="Bold"></Setter> <Setter Property="Height" Value="25"></Setter> <Setter Property="Width" Value="100"></Setter> </Style.Setters> <Style.Triggers> <Trigger Property="IsFocused" Value="True"> <Setter Property="Background" Value="Purple"></Setter> <Setter Property="Foreground" Value="DarkCyan"></Setter> <Setter Property="FontFamily" Value="Franklin Gothic"></Setter> <Setter Property="FontSize" Value="10"></Setter> <Setter Property="FontWeight" Value="Normal"></Setter> <Setter Property="Height" Value="50"></Setter> <Setter Property="Width" Value="200"></Setter> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="Red"></Setter> <Setter Property="Foreground" Value="White"></Setter> <Setter Property="FontFamily" Value="Calibri"></Setter> <Setter Property="FontSize" Value="25"></Setter> <Setter Property="FontWeight" Value="Heavy"></Setter> <Setter Property="Height" Value="100"></Setter> <Setter Property="Width" Value="400"></Setter> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter Property="Background" Value="Green"></Setter> <Setter Property="Foreground" Value="Violet"></Setter> <Setter Property="FontFamily" Value="Times New Roman"></Setter> <Setter Property="FontSize" Value="20"></Setter> <Setter Property="FontWeight" Value="Thin"></Setter> <Setter Property="Height" Value="250"></Setter> <Setter Property="Width" Value="250"></Setter> </Trigger> </Style.Triggers> </Style> </Window.Resources> <Button>It a Magic.</Button> 

0
source

All Articles