EventTrigger 类


表示一个触发器,该触发器应用一组操作以响应一个事件。Represents a trigger that applies a set of actions in response to an event.

public ref class EventTrigger : System::Windows::TriggerBase, System::Windows::Markup::IAddChild
public class EventTrigger : System.Windows.TriggerBase, System.Windows.Markup.IAddChild
type EventTrigger = class
    inherit TriggerBase
    interface IAddChild
Public Class EventTrigger
Inherits TriggerBase
Implements IAddChild


此示例演示如何在样式中使用事件触发器来对的和事件进行动画处理 MouseEnter MouseLeave FrameworkElementThis example shows how to use event triggers in a style to animate the MouseEnter and MouseLeave events of a FrameworkElement. 在此示例中,将 Style TargetType 设置为 RectangleIn this example, the Style has the TargetType set to Rectangle. 因此,不需要 MouseEnter 用类名来限定和 MouseLeave 事件名称。Therefore, there is no need to qualify the MouseEnter and MouseLeave event names with the class name.

<Style TargetType="Rectangle">
  <Setter Property="Width" Value="50" />
  <Setter Property="Height" Value="50" />
  <Setter Property="Margin" Value="20" />
  <Setter Property="HorizontalAlignment" Value="Left" />
    <EventTrigger RoutedEvent="MouseEnter">
              <DoubleAnimation To="300" Duration="0:0:1.5" 
                AccelerationRatio="0.10" DecelerationRatio="0.25" 
                Storyboard.TargetProperty="(Canvas.Width)" />
    <EventTrigger RoutedEvent="MouseLeave">
              <DoubleAnimation Duration="0:0:1.5" 
                AccelerationRatio="0.10" DecelerationRatio="0.25" 
                Storyboard.TargetProperty="(Canvas.Width)" />


Trigger 对象具有 SettersEnterActions 和属性, ExitActions 这些属性基于特定属性的状态应用更改或操作,而对象在 EventTrigger 指定的 Actions 路由事件发生时启动一组。Trigger objects have the Setters, EnterActions, and ExitActions properties that apply changes or actions based on the state of certain properties, while EventTrigger objects start a set of Actions when a specified routed event occurs. 例如,你可能希望使用 EventTrigger 来在鼠标指针位于特定用户界面 (UI) 控件上时开始一组动画。For example, you may want to use an EventTrigger to start a set of animations when the mouse pointer is over a certain user interface (UI) control. Trigger 不同 EventTrigger 的是,没有状态终止的概念,因此,一旦引发事件的条件不再为 true,该操作将不会撤消。Unlike Trigger, EventTrigger has no concept of termination of state, so the action will not be undone once the condition that raised the event is no longer true.

请注意,使用时 EventTrigger ,需要选择不会影响控件固有行为的事件。Note that when using an EventTrigger, you need to choose events that do not interfere with the inherent behavior of your control. 控件(例如 Button 或) TextBox 对用户输入事件执行特定操作,例如鼠标单击和键盘事件。Controls such as Button or TextBox perform specific actions on user input events such as mouse clicks and keyboard events. 例如,如果对按钮进行样式设置并尝试将 MouseDown 事件设置为 RoutedEventEventTrigger ,则 EventTrigger 永远不会应用,因为该事件首先由按钮处理。For example, if you are styling a button and try to set the MouseDown event as the RoutedEvent of an EventTrigger, the EventTrigger never gets applied because the event first gets handled by the button. 您可以改为使用 PreviewMouseDown 事件或其他事件。Instead, you can use the PreviewMouseDown event or a different event.

使用数据绑定时,如果使用的是 TargetUpdated 事件,则必须将对象的值设置为,以便 NotifyOnTargetUpdated Binding true 引发事件。When using data binding, if you are using the TargetUpdated event, you must set the NotifyOnTargetUpdated value of your Binding object to true for the event to be raised.

如果将 TriggerAction 子对象添加到对象中,则 EventTrigger 会将其隐式添加到 TriggerActionCollection 对象的 EventTriggerAdding a TriggerAction child to an EventTrigger object implicitly adds it to the TriggerActionCollection for the EventTrigger object.



初始化 EventTrigger 类的新实例。Initializes a new instance of the EventTrigger class.


使用指定的事件初始化 EventTrigger 类的新实例。Initializes a new instance of the EventTrigger class with the specified event.



获取事件发生时要应用的操作的集合。Gets the collection of actions to apply when the event occurs.


获取 DependencyObjectType 包装此实例的 CLR 类型的。Gets the DependencyObjectType that wraps the CLR type of this instance.

(继承自 DependencyObject)

获取与此 Dispatcher 关联的 DispatcherObjectGets the Dispatcher this DispatcherObject is associated with.

(继承自 DispatcherObject)

获取要在触发器对象变为活动状态时应用的 TriggerAction 对象的集合。Gets a collection of TriggerAction objects to apply when the trigger object becomes active. 此属性不适用于 EventTrigger 类。This property does not apply to the EventTrigger class.

(继承自 TriggerBase)

获取要在触发器对象变为非活动状态时应用的 TriggerAction 对象的集合。Gets a collection of TriggerAction objects to apply when the trigger object becomes inactive. 此属性不适用于 EventTrigger 类。This property does not apply to the EventTrigger class.

(继承自 TriggerBase)

获取一个值,该值指示此实例当前是否为密封的(只读)。Gets a value that indicates whether this instance is currently sealed (read-only).

(继承自 DependencyObject)

获取或设置将激活该触发器的 RoutedEventGets or sets the RoutedEvent that will activate this trigger.


获取或设置对象的名称,该对象具有激活此触发器的事件。Gets or sets the name of the object with the event that activates this trigger. 仅元素触发器或模板触发器可使用此名称。This is only used by element triggers or template triggers.



将指定的对象添加到当前事件触发器的 Actions 集合中。Adds the specified object to the Actions collection of the current event trigger.


此方法不受支持并导致异常。This method is not supported and results in an exception.


确定调用线程是否可以访问此 DispatcherObjectDetermines whether the calling thread has access to this DispatcherObject.

(继承自 DispatcherObject)

清除属性的本地值。Clears the local value of a property. 要清除的属性由 DependencyProperty 标识符指定。The property to be cleared is specified by a DependencyProperty identifier.

(继承自 DependencyObject)

清除只读属性的本地值。Clears the local value of a read-only property. 要清除的属性由 DependencyPropertyKey 指定。The property to be cleared is specified by a DependencyPropertyKey.

(继承自 DependencyObject)

对指定依赖属性的值进行强制。Coerces the value of the specified dependency property. 通过对调用方 DependencyObject 上存在的依赖属性的属性元数据中所指定的任何 CoerceValueCallback 函数进行调用来完成此操作。This is accomplished by invoking any CoerceValueCallback function specified in property metadata for the dependency property as it exists on the calling DependencyObject.

(继承自 DependencyObject)

确定提供的 DependencyObject 是否等效于当前 DependencyObjectDetermines whether a provided DependencyObject is equivalent to the current DependencyObject.

(继承自 DependencyObject)

获取此 DependencyObject 的哈希代码。Gets a hash code for this DependencyObject.

(继承自 DependencyObject)

创建一个专用的枚举数,用于确定哪些依赖项属性在此 DependencyObject 上具有以本地方式设置的值。Creates a specialized enumerator for determining which dependency properties have locally set values on this DependencyObject.

(继承自 DependencyObject)

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

DependencyObject 的此实例返回依赖属性的当前有效值。Returns the current effective value of a dependency property on this instance of a DependencyObject.

(继承自 DependencyObject)

重新评估指定依赖属性的有效值。Re-evaluates the effective value for the specified dependency property.

(继承自 DependencyObject)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

每当更新此 DependencyObject 的任何依赖属性的有效值时调用。Invoked 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.

(继承自 DependencyObject)

如果存在,则返回依赖属性的本地值。Returns the local value of a dependency property, if it exists.

(继承自 DependencyObject)
SetCurrentValue(DependencyProperty, Object)

设置依赖属性的值而不更改其值源。Sets the value of a dependency property without changing its value source.

(继承自 DependencyObject)
SetValue(DependencyProperty, Object)

设置依赖属性的本地值,该值由其依赖属性标识符指定。Sets the local value of a dependency property, specified by its dependency property identifier.

(继承自 DependencyObject)
SetValue(DependencyPropertyKey, Object)

设置一个只读依赖属性的本地值,该值由依赖属性的 DependencyPropertyKey 标识符指定。Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property.

(继承自 DependencyObject)

返回序列化进程是否应序列化此类的实例上 Actions 属性的有效值。Returns whether serialization processes should serialize the effective value of the Actions property on instances of this class.


返回一个值,该值指示序列化进程是否应序列化所提供的依赖属性的值。Returns a value that indicates whether serialization processes should serialize the value for the provided dependency property.

(继承自 DependencyObject)

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

强制调用线程具有此 DispatcherObject 的访问权限。Enforces that the calling thread has access to this DispatcherObject.

(继承自 DispatcherObject)



添加子对象。Adds a child object.


将节点的文本内容添加到对象。Adds the text content of a node to the object.