3 out of 4 rated this helpful - Rate this topic

Trigger Class

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/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
'Declaration
<ContentPropertyAttribute("Setters")> _
<XamlSetTypeConverterAttribute("ReceiveTypeConverter")> _
Public Class Trigger _
	Inherits TriggerBase _
	Implements IAddChild, ISupportInitialize
<Trigger>
  Setters
</Trigger>

The Trigger type exposes the following members.

  NameDescription
Public methodTriggerInitializes a new instance of the Trigger class.
Top
  NameDescription
Public propertyDependencyObjectTypeGets the DependencyObjectType that wraps the CLR type of this instance.  (Inherited from DependencyObject.)
Public propertyDispatcherGets the Dispatcher this DispatcherObject is associated with. (Inherited from DispatcherObject.)
Public propertyEnterActionsGets a collection of TriggerAction objects to apply when the trigger object becomes active. This property does not apply to the EventTrigger class. (Inherited from TriggerBase.)
Public propertyExitActionsGets a collection of TriggerAction objects to apply when the trigger object becomes inactive. This property does not apply to the EventTrigger class. (Inherited from TriggerBase.)
Public propertyIsSealedGets a value that indicates whether this instance is currently sealed (read-only). (Inherited from DependencyObject.)
Public propertyPropertyGets or sets the property that returns the value that is compared with the Value property of the trigger. The comparison is a reference equality check.
Public propertySettersGets a collection of Setter objects, which describe the property values to apply when the specified condition has been met.
Public propertySourceNameGets or sets the name of the object with the property that causes the associated setters to be applied.
Public propertyValueGets or sets the value to be compared with the property value of the element. The comparison is a reference equality check.
Top
  NameDescription
Public methodCheckAccessDetermines whether the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Public methodClearValue(DependencyProperty)Clears the local value of a property. The property to be cleared is specified by a DependencyProperty identifier. (Inherited from DependencyObject.)
Public methodClearValue(DependencyPropertyKey)Clears the local value of a read-only property. The property to be cleared is specified by a DependencyPropertyKey. (Inherited from DependencyObject.)
Public methodCoerceValueCoerces the value of the specified dependency property. This is accomplished by invoking any CoerceValueCallback function specified in property metadata for the dependency property as it exists on the calling DependencyObject. (Inherited from DependencyObject.)
Public methodEqualsDetermines whether a provided DependencyObject is equivalent to the current DependencyObject. (Inherited from DependencyObject.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeGets a hash code for this DependencyObject. (Inherited from DependencyObject.)
Public methodGetLocalValueEnumeratorCreates a specialized enumerator for determining which dependency properties have locally set values on this DependencyObject. (Inherited from DependencyObject.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetValueReturns the current effective value of a dependency property on this instance of a DependencyObject. (Inherited from DependencyObject.)
Public methodInvalidatePropertyRe-evaluates the effective value for the specified dependency property (Inherited from DependencyObject.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodOnPropertyChangedInvoked whenever the effective value of any dependency property on this DependencyObject has been updated. The specific dependency property that changed is reported in the event data. (Inherited from DependencyObject.)
Public methodReadLocalValueReturns the local value of a dependency property, if it exists. (Inherited from DependencyObject.)
Public methodStatic memberReceiveTypeConverterHandles cases where a type converter provides a value for a property of a Trigger object.
Public methodSetCurrentValueSets the value of a dependency property without changing its value source. (Inherited from DependencyObject.)
Public methodSetValue(DependencyProperty, Object)Sets the local value of a dependency property, specified by its dependency property identifier. (Inherited from DependencyObject.)
Public methodSetValue(DependencyPropertyKey, Object)Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property. (Inherited from DependencyObject.)
Protected methodShouldSerializePropertyReturns a value that indicates whether serialization processes should serialize the value for the provided dependency property. (Inherited from DependencyObject.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodVerifyAccessEnforces that the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Top
  NameDescription
Explicit interface implemetationPrivate methodIAddChild.AddChildAdds a child object.
Explicit interface implemetationPrivate methodIAddChild.AddTextAdds the text content of a node to the object.
Explicit interface implemetationPrivate methodISupportInitialize.BeginInitInfrastructure. Signals the object that initialization is starting.
Explicit interface implemetationPrivate methodISupportInitialize.EndInitInfrastructure. Signals the object that initialization is complete.
Top

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 MultiDataTrigger 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.


<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 ChangesThis 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.

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.