Trigger Class

Represents a trigger that applies property values or performs actions conditionally.

Namespace: System.Windows
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:

public class Trigger : TriggerBase, IAddChild
/** @attribute ContentPropertyAttribute("Setters") */ 
public class Trigger extends TriggerBase implements IAddChild
public class Trigger extends TriggerBase implements IAddChild

WPF defines properties that correspond to end-user actions, such as the IsMouseOver property that is set to true when the user hovers the cursor over a UIElement or the corresponding IsMouseOver property of a ContentElement. Representing end-user actions in property values, along with the Trigger element, allows WPF styles to change property values based on those end-user actions, all from within markup.

The properties changed by triggers are automatically reset to their previous value when the triggered condition is no longer satisfied. Triggers are optimized for transient states which are expected to change and return to original state, such as IsPressed on Button and IsSelected on ListBoxItem. The Property of interest must be a dependency property.

Note that you must specify both the Property and Value properties on a Trigger for the trigger to be meaningful. If one or both of the properties are not set, an exception is thrown.

The Setters property of a Trigger object can only consist of Setter objects. Adding a Setter child to a Trigger object implicitly adds it to the SetterBaseCollection for the Trigger object. EventSetter objects are not supported; only Style.Setters supports EventSetter objects.

There are other types of triggers. MultiTrigger allows you to apply changes based on the state of multiple properties. EventTrigger allows you to apply changes when an event occurs. DataTrigger and MultiTrigger are for data-bound properties.

The following example contains two triggers. The first changes the Background property of a Button when the IsMouseOver property is true. The second changes the Foreground property of a button when the IsPressed property is true.

<Style x:Key="Triggers" TargetType="Button">
    <Trigger Property="IsMouseOver" Value="true">
        <Setter Property = "Background" Value="Red"/>
    <Trigger Property="IsPressed" Value="true">
        <Setter Property = "Foreground" Value="Green"/>

More Code

How to: Trigger an Animation When a Property Value Changes

This example shows how to use a Trigger to start a Storyboard when a property value changes. You can use a Trigger inside a Style, ControlTemplate, or DataTemplate.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0