EventTrigger Class


代表套用一組動作以回應事件的觸發程序。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


這個範例示範如何使用樣式中的事件觸發程式,以動畫顯示 FrameworkElementMouseEnterMouseLeave 事件。This example shows how to use event triggers in a style to animate the MouseEnter and MouseLeave events of a FrameworkElement. 在此範例中,StyleTargetType 設定為 [Rectangle]。In this example, the Style has the TargetType set to Rectangle. 因此,不需要使用類別名稱來限定 MouseEnterMouseLeave 的事件名稱。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 物件具有 SettersEnterActionsExitActions 屬性,可根據特定屬性的狀態套用變更或動作,而 EventTrigger 物件則會在指定的路由事件發生時啟動一組 ActionsTrigger 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. 例如,當滑鼠指標停留在特定的 使用者介面 (UI)user interface (UI) 控制項上時,您可能會想要使用 EventTrigger 來啟動一組動畫。For example, you may want to use an EventTrigger to start a set of animations when the mouse pointer is over a certain 使用者介面 (UI)user interface (UI) control. 不同于 TriggerEventTrigger 沒有狀態終止的概念,因此一旦引發事件的條件不再為 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. 控制項(例如 ButtonTextBox)會對使用者輸入事件(例如滑鼠點按和鍵盤事件)執行特定動作。Controls such as Button or TextBox perform specific actions on user input events such as mouse clicks and keyboard events. 例如,如果您要設定按鈕的樣式,並嘗試將 MouseDown 事件設為 EventTriggerRoutedEvent,則永遠不會套用 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 事件,則必須將 Binding 物件的 NotifyOnTargetUpdated 值設定為 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 物件中,會隱含地將它加入 EventTrigger 物件的 TriggerActionCollection 中。Adding 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.


取得包裝此執行個體之 CLRCLR 型別的 DependencyObjectTypeGets the DependencyObjectType that wraps the CLRCLR type of this instance.

(Inherited from DependencyObject)

取得與這個 Dispatcher 關聯的 DispatcherObjectGets the Dispatcher this DispatcherObject is associated with.

(Inherited from 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.

(Inherited from 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.

(Inherited from TriggerBase)

取得值,這個值表示此執行個體目前是否已密封 (唯讀)。Gets a value that indicates whether this instance is currently sealed (read-only).

(Inherited from 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.

(Inherited from DispatcherObject)

清除屬性的區域數值。Clears the local value of a property. 要清除的屬性是由 DependencyProperty 識別項所指定。The property to be cleared is specified by a DependencyProperty identifier.

(Inherited from DependencyObject)

清除唯讀屬性的區域數值。Clears the local value of a read-only property. 要清除的屬性是由 DependencyPropertyKey 所指定。The property to be cleared is specified by a DependencyPropertyKey.

(Inherited from 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.

(Inherited from DependencyObject)

判斷提供的 DependencyObject 和目前的 DependencyObject 是否相等。Determines whether a provided DependencyObject is equivalent to the current DependencyObject.

(Inherited from DependencyObject)

取得這個 DependencyObject 的雜湊碼。Gets a hash code for this DependencyObject.

(Inherited from DependencyObject)

建立特定的列舉值,以判斷哪些相依性屬性在此 DependencyObject 上具有本機設定的值。Creates a specialized enumerator for determining which dependency properties have locally set values on this DependencyObject.

(Inherited from DependencyObject)

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)

傳回 DependencyObject 的這個執行個體上之相依性屬性的目前有效值。Returns the current effective value of a dependency property on this instance of a DependencyObject.

(Inherited from DependencyObject)

重新評估指定相依性屬性的有效值。Re-evaluates the effective value for the specified dependency property.

(Inherited from DependencyObject)

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(Inherited from 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.

(Inherited from DependencyObject)

傳回相依性屬性的區域值 (如果存在)。Returns the local value of a dependency property, if it exists.

(Inherited from DependencyObject)
SetCurrentValue(DependencyProperty, Object)

設定相依性屬性的值,而不需要變更其值來源。Sets the value of a dependency property without changing its value source.

(Inherited from DependencyObject)
SetValue(DependencyProperty, Object)

設定相依性屬性的區域值 (由相依性屬性的識別碼所指定)。Sets the local value of a dependency property, specified by its dependency property identifier.

(Inherited from DependencyObject)
SetValue(DependencyPropertyKey, Object)

設定唯讀相依性屬性的區域數值 (由相依性屬性的 DependencyPropertyKey 識別項所指定)。Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property.

(Inherited from 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.

(Inherited from DependencyObject)

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

請強制執行可以存取這個 DispatcherObject 的呼叫執行緒。Enforces that the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)

Explicit Interface Implementations


加入子物件。Adds a child object.


將節點的文字內容加入物件中。Adds the text content of a node to the object.

Applies to

See also