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
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. 该事件仍会传递路由的其余部分,但只会在响应中调用专门添加到 AddHandler(RoutedEvent, Delegate, Boolean) 方法调用中 HandledEventsToo true 的处理程序。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 更改为 false来修改事件参数。In 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.