RoutedEventArgs.Handled 屬性

定義

取得或設定值,這個值表示路由事件在傳送路由時之事件處理的目前狀態。

public:
 property bool Handled { bool get(); void set(bool value); };
public bool Handled { [System.Security.SecurityCritical] get; [System.Security.SecurityCritical] set; }
public bool Handled { get; set; }
[<get: System.Security.SecurityCritical>]
[<set: System.Security.SecurityCritical>]
member this.Handled : bool with get, set
member this.Handled : bool with get, set
Public Property Handled As Boolean

屬性值

設定時,如果事件是要標記為已處理,則請設定為 true ;否則為 false。 如果讀取這個值, true 表示類別處理常式或路由中的某個執行個體處理常式已將這個事件標記為已處理。 false 表示沒有這類處理常式已將事件標記為已處理。

預設值是 false

屬性

範例

下列範例會實作事件處理常式,以標記已處理的事件。

protected override void OnPreviewMouseRightButtonDown(System.Windows.Input.MouseButtonEventArgs e)
{
    e.Handled = true; //suppress the click event and other leftmousebuttondown responders
    MyEditContainer ec = (MyEditContainer)e.Source;
    if (ec.EditState)
    { ec.EditState = false; }
    else
    { ec.EditState = true; }
    base.OnPreviewMouseRightButtonDown(e);
}
Protected Overrides Sub OnPreviewMouseRightButtonDown(ByVal e As System.Windows.Input.MouseButtonEventArgs)
    e.Handled = True 'suppress the click event and other leftmousebuttondown responders
    Dim ec As MyEditContainer = CType(e.Source, MyEditContainer)
    If ec.EditState Then
        ec.EditState = False
    Else
        ec.EditState = True
    End If
    MyBase.OnPreviewMouseRightButtonDown(e)
End Sub

備註

標記已處理的事件將會限制路由事件的可見度,以沿著事件路由接聽程式。 事件仍然會移動路由的其餘部分,但只會在方法呼叫中 AddHandler(RoutedEvent, Delegate, Boolean) 特別新增 HandledEventsTootrue 的處理常式叫用回應。 實例接聽程式上的預設處理常式 (,例如,不會叫用以可延伸應用程式標記語言 (XAML) ) 表示的處理常式。 處理標示為已處理的事件不是常見的案例。

如果您是定義您自己的事件的控制項作者,您針對類別層級的事件處理所做的決策會影響控制項的使用者,以及衍生控制項的任何使用者,以及可能包含您控制項或包含控制項的其他元素。 如需詳細資訊,請參閱將路由事件標記為已處理以及類別處理

在非常罕見的情況下,適當地處理標示 trueHandled 的事件,並藉由變更 Handledfalse 來修改事件引數。 這在控制項的輸入事件的特定區域中可能是必要的,例如的索引鍵處理 KeyDown ,以及 TextInput 低階和高階輸入事件競爭處理,而且每一個都嘗試使用不同的路由策略。

適用於