RoutedEventArgs.Handled 属性


获取或设置一个值,该值指示针对路由事件(在其经过路由时)的事件处理的当前状态。Gets or sets a value that indicates the present state of the event handling for a routed event as it travels the route.

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



设置的时候,如果事件将标记为已处理,则设置为 true ;否则为 falseIf setting, set to true if the event is to be marked handled; otherwise false. 如果读取此值, true 指示沿路由的一个类处理程序或某个实例处理程序已将此事件标记为已处理。If reading this value, true indicates that either a class handler, or some instance handler along the route, has already marked this event handled. false.指示没有这类处理程序将该事件标记为已处理。false.indicates that no such handler has marked the event handled.

默认值为 falseThe default value is false.



下面的示例实现一个将事件标记为已处理的事件处理程序。The following example implements an event handler that marks the event handled.

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; }
    { ec.EditState = true; }
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
        ec.EditState = True
    End If
End Sub


将事件标记为已处理会将路由事件的可见性限制为沿着事件路由的侦听器。Marking the event handled will limit the visibility of the routed event to listeners along the event route. 该事件仍会传递路由的其余部分,但只有在方法调用中专门添加的处理程序才会 HandledEventsToo true AddHandler(RoutedEvent, Delegate, Boolean) 在响应中调用。The event does still travel the remainder of the route, but only handlers specifically added with HandledEventsToo true in the AddHandler(RoutedEvent, Delegate, Boolean) method call will be invoked in response. 实例侦听器上的默认处理程序 (例如,在) 中表示的处理程序 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML) 将不会被调用。Default handlers on instance listeners (such as those expressed in 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML)) will not be invoked. 处理标记为已处理的事件并非常见方案。Handling events that are marked handled is not a common scenario.

如果你是控件作者定义你自己的事件,那么,在类级别对事件处理做出的决策将影响你的控件的用户以及任何派生控件的用户,以及可能包含在你的控件中或包含控件的其他元素。If you are a control author defining your own events, the decisions you make regarding event handling at the class level will impact users of your control as well as any users of derived controls, and potentially other elements that are either contained by your control or that contain your control. 有关详细信息,请参阅将路由事件标记为“已处理”和类处理For more information, see Marking Routed Events as Handled, and Class Handling.

在极少数情况下,适合处理标记为的事件 Handled true ,并通过将更改为来修改事件 Handled 参数 falseIn very rare circumstances it is appropriate to handle events where Handled is marked true, and modify the event arguments by changing Handled to false. 在控件输入事件的某些区域中,这可能是必需的,例如,对的键处理方式为, KeyDown TextInput 其中,低级别和高级别的输入事件争用处理,每个都尝试使用不同的路由策略。This can be necessary in certain areas of input events of controls, such as key handling of KeyDown versus TextInput where low level and high level input events compete for the handling, and each is attempting to work with a different routing strategy.