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

備註

標記已處理的事件會限制沿途聽眾對所選活動的可見度。 事件仍會傳送路徑的其餘部分,但只有在方法呼叫中HandledEventsToo特別加入的trueAddHandler(RoutedEvent, Delegate, Boolean)處理器會被回應。 實例監聽器上的預設處理程序(例如以可擴充應用程式標記語言(XAML)表達的)將不會被呼叫。 處理標記已處理的事件並不常見。

如果你是控制權作者,自行定義自己的事件,你在類別層級事件處理上的決策會影響你控制的使用者,以及任何衍生控制項的使用者,甚至可能包含在你控制項或控制控制中的其他元素。 欲了解更多資訊,請參閱 「標記路由事件為已處理」及「類別處理」。

在極少數情況下,處理標記Handledtrue為 的事件是適當的,並透過將事件參數改為 Handledfalse來修改。 這在控制的某些輸入事件領域中是必要的,例如對 vs KeyDown 的鍵處理TextInput,當低層與高階輸入事件爭奪處理權時,雙方嘗試採用不同的路由策略。

適用於