Creating a property trigger
So far we've used styles that have a collection of setters. A Style
also has a Triggers
collection property that provides a declarative way to make some property changes without a need for actual code. As such triggers are part of a style, they are naturally applied to more than one element. There are three types of triggers supported by WPF, and in this recipe we'll examine the simplest one - the property trigger.
Getting ready
Open the CH08.StyledCalculator
project.
How to do it...
We'll add an effect when clicking a button in the calculator project using a property trigger:
We want to add a trigger to one of the styles we created in an earlier recipe. Open
MainWindow.xaml
and look for the style with the keynumericStyle
.Add a
Triggers
property inside the style:<Style.Triggers> </Style.Triggers>
Add a property trigger inside the style (after the setters, before the closing style tag). The trigger will be invoked when the
IsPressed
property is changed to...