ContentElement.OnMouseDown(MouseButtonEventArgs) 方法


当某个未处理的 MouseDown 附加事件在其路由中到达派生自此类的某个元素时调用。Invoked when an unhandled MouseDown attached event reaches an element in its route that is derived from this class. 实现此方法可为此事件添加类处理。Implement this method to add class handling for this event.

protected public:
 virtual void OnMouseDown(System::Windows::Input::MouseButtonEventArgs ^ e);
protected internal virtual void OnMouseDown (System.Windows.Input.MouseButtonEventArgs e);
abstract member OnMouseDown : System.Windows.Input.MouseButtonEventArgs -> unit
override this.OnMouseDown : System.Windows.Input.MouseButtonEventArgs -> unit
Protected Friend Overridable Sub OnMouseDown (e As MouseButtonEventArgs)



包含事件数据的 MouseButtonEventArgsThe MouseButtonEventArgs that contains the event data. 此事件数据报告有关按下鼠标按钮和已处理状态的详细信息。This event data reports details about the mouse button that was pressed and the handled state.


鼠标按钮操作也由专用的输入管理器进行处理。Mouse button actions are also handled by a dedicated input manager. 依赖于鼠标按钮操作的其他编程功能(如输入和命令绑定)可能会调用操作的处理程序,然后才能将其作为常规鼠标按钮操作公开。Other programming features that rely on mouse button actions, such as input and command bindings, might invoke handlers for the action before it is exposed as a general mouse button action. 如果这些输入系统功能将 Mouse.MouseDown 事件标记为已处理, OnMouseDown 则不会调用。If these input system features mark the Mouse.MouseDown event as handled, OnMouseDown is not invoked.

如果使用此类处理程序将事件标记为已处理,则可能会影响以下事件: MouseLeftButtonDownMouseRightButtonDownIf you use this class handler to mark the event as handled, you potentially impact the following events: MouseLeftButtonDown and MouseRightButtonDown. 收到时,可能会在接收元素上引发这些事件中的任何一个 Mouse.MouseDownEither of these events may be raised on the receiving element when Mouse.MouseDown is received.

如果将此事件标记为 "在类处理中处理",则仍会引发 subevents;但是,它们将在事件数据中传递已处理状态。If you mark this event as handled in class handling, subevents are still raised; however, they pass the handled state in the event data. 如果在类处理中处理该事件,则不会调用 subevents 的实例处理程序,除非您显式使用 AddHandler(RoutedEvent, Delegate, Boolean) with 以便 handledEventsToo true 附加处理程序。If the event is handled in class handling, instance handlers for the subevents are not invoked unless you explicitly use AddHandler(RoutedEvent, Delegate, Boolean) with handledEventsToo true in order to attach handlers. 类处理程序也不会被调用,除非这些类处理程序已注册到 RegisterClassHandler(Type, RoutedEvent, Delegate, Boolean) 签名 handledEventsToo trueClass handlers also are not invoked unless those class handlers were registered with the RegisterClassHandler(Type, RoutedEvent, Delegate, Boolean) signature with handledEventsToo true. 通过处理 OnMouseDown ,你可以保证类处理对所有可能的鼠标按钮按下操作都适用。By handling OnMouseDown, you are implying that your class handling accounted for all possible mouse button down actions. 此行为可能不是必需的;因此,使用此虚方法将事件标记为已处理时,请谨慎使用。This behavior might be unwanted; therefore, use caution when you use this virtual method to mark events as handled.

每个特定于按钮的直接事件还具有一个虚拟 On * 方法; 请考虑是否可以更适当地重写这些按钮特定的类处理程序。Each of the button-specific direct events also has a virtual On* method; consider whether overriding these button-specific class handlers might be more appropriate.

此方法没有默认实现。This method has no default implementation. 因为继承中的中间类可能实现此方法,所以建议您在实现中调用基实现。Because an intermediate class in the inheritance might implement this method, we recommend that you call the base implementation in your implementation.

此方法的用途类似于 公共语言运行时 (CLR)common language runtime (CLR) 方法的事件模式 * :此方法提供了通过建立类处理程序而不是实例处理程序来处理来自派生类的匹配事件的方法。The purpose of this method is similar to the 公共语言运行时 (CLR)common language runtime (CLR) event pattern On* methods: this method provides the means to handle the matching event from derived classes by establishing a class handler instead of an instance handler. 在这种情况下,匹配事件是路由事件。In this case the matching event is a routed event. *对于路由事件,On 方法的实现模式是不同的,因为路由事件可以由子元素引发,这不一定是将调用处理程序的元素。The implementation pattern of the On* methods is different for routed events because a routed event can be raised by a child element, which is not necessarily the element that will invoke handlers. 因此,您的实现需要检查事件数据的源属性。Therefore, your implementation needs to examine the source properties of the event data. 大多数情况下,它不应尝试 reraise 该事件。It should not try to reraise the event in most cases.

通过重写此方法或使用注册类处理程序 RegisterClassHandlerContentElement 可以在事件路由中收到事件时调用私有类处理程序方法。Either by overriding this method or by registering class handlers with RegisterClassHandler, derived classes of ContentElement can call private class handler methods when the event is received along the event route. 适用于类处理的一种情况是操作事件数据,并将路由事件标记为 "已处理"。One scenario where class handling is appropriate is to manipulate the event data and mark the routed event as handled.