Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Trigger Class
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Trigger Class

Updated: November 2007

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

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/xaml/presentation

Visual Basic (Declaration)
<ContentPropertyAttribute("Setters")> _
Public Class Trigger _
    Inherits TriggerBase _
    Implements IAddChild
Visual Basic (Usage)
Dim instance As Trigger
C#
[ContentPropertyAttribute("Setters")]
public class Trigger : TriggerBase, IAddChild
Visual C++
[ContentPropertyAttribute(L"Setters")]
public ref class Trigger : public TriggerBase, 
    IAddChild
J#
/** @attribute ContentPropertyAttribute("Setters") */
public class Trigger extends TriggerBase implements IAddChild
JScript
public class Trigger extends TriggerBase implements IAddChild
XAML Object Element Usage
<Trigger>
  Setters
</Trigger>

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 shows a named Style available to Button controls. The Style defines a Trigger element that changes the Foreground property of a button when the IsPressed property is true.

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

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 Vista

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker