Mouse.MouseDown 附加事件

定义

当按下任何鼠标按钮时发生。Occurs when any mouse button is depressed.

see AddMouseDownHandler, and RemoveMouseDownHandler
see AddMouseDownHandler, and RemoveMouseDownHandler
see AddMouseDownHandler, and RemoveMouseDownHandler

注解

若要确定鼠标按下了哪个按钮,请检查传递到处理程序的 MouseButtonEventArgs 中的 ChangedButton 属性。To determine which mouse button was depressed, check the ChangedButton property in the MouseButtonEventArgs passed to the handler.

这是一个附加事件。This is an attached event. WPFWPF 将附加事件作为路由事件实现。implements attached events as routed events. 附加事件本质上是一种用于引用事件的 XAMLXAML 语言概念,这些事件可在未定义该事件的对象上进行处理,这 WPFWPF 通过同时启用事件来遍历路由来进行扩展。Attached events are fundamentally a XAMLXAML language concept for referencing events that can be handled on objects that do not define that event, which WPFWPF expands upon by also enabling the event to traverse a route. 附加事件在代码中没有直接处理语法;若要在代码中附加路由事件的处理程序,请使用指定的 Add * 处理程序方法。Attached events do not have a direct handling syntax in code; to attach handlers for a routed event in code, you use a designated Add*Handler method. 有关详细信息,请参阅附加事件概述For details, see Attached Events Overview.

Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 框架在此附加事件的基础上构建,方法是在 UIElementContentElement上将其呈现为两个不同的 公共语言运行时 (CLR)common language runtime (CLR) 事件: MouseLeftButtonDownMouseRightButtonDownThe Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) framework builds on this attached event by surfacing it as two different 公共语言运行时 (CLR)common language runtime (CLR) events on UIElement and ContentElement: MouseLeftButtonDown and MouseRightButtonDown. 这些实现处理基础 MouseDown 事件并读取事件的参数,以确定是否涉及鼠标左键或右键。These implementations handle the underlying MouseDown event and read the arguments of the event to determine whether the left or right mouse button was involved. 对于三按钮鼠标,中间按钮没有框架级别的事件支持。For a three-button mouse, there is no framework-level event support for the center button. 应使用 MouseDown 事件,并在事件参数中检查 MiddleButton 状态。You should use the MouseDown event and check the MiddleButton state in the event arguments.

重要

具有类似于控件的行为的几个 ContentElement 派生类(例如,Hyperlink)可能具有鼠标按钮事件的固有类处理。A few ContentElement derived classes that have control-like behavior, for example, Hyperlink, might have inherent class handling for mouse button events. 鼠标左键按下事件是在控件中具有类处理的最可能的事件。The left mouse button down event is the most likely event to have class handling in a control. 类处理通常将基础 Mouse 类事件标记为已处理。The class handling often marks the underlying Mouse class event as handled. 将该事件标记为已处理后,通常不会引发附加到该元素的其他实例处理程序。Once the event is marked handled, other instance handlers that are attached to that element are not ordinarily raised. 通常也不会引发在冒泡方向上沿 UI 树中的根向下附加到元素的任何其他类或实例处理程序。Any other class or instance handlers that are attached to elements in the bubbling direction towards the root in the UI tree are also not ordinarily raised.

您可以通过使用以下任一解决方案来解决上述重要说明中列出的问题,并在具有类处理的派生类上接收鼠标左键按下事件 MouseDown 事件:You can resolve the issue that is outlined in the preceding Important note and still receive MouseDown events for left mouse button down events on a derived class that has class handling by using either of these solutions:

  • PreviewMouseDown 事件附加处理程序,这些处理程序未标记为由控件处理。Attach handlers for the PreviewMouseDown event, which is not marked as handled by the controls. 请注意,因为这是预览事件,所以路由从根节点开始,向下隧道到控件。Notice that because this is a preview event, the route starts at the root and tunnels down to the control.

  • 通过调用 AddHandler 并选择允许处理程序侦听事件的签名选项(即使它们已在路由事件数据中标记为已处理),在控件过程上注册处理程序。Register a handler on the control procedurally by calling AddHandler and choosing the signature option that enables handlers to listen for events even if they are already marked as handled in the routed event data.

对于与鼠标相关的路由事件,请注意如何或何时将其标记为已处理。For routed events that relate to the mouse, be careful about how or when you mark them handled. 对于是否还应告知父元素是否还应告知父元素是否还应告知父元素的问题,这一难题非常困难,原因是 WPFWPF 框架选择了将基础鼠标路由事件呈现为沿路由 CLRCLR 事件的模型。The difficulty in making the appropriate choices about whether parent elements should also be informed about any given mouse action is in fact why the WPFWPF framework chose the model of having the underlying mouse routed event be surfaced as CLRCLR events along the route. 隧道鼠标事件存在类似问题。Similar issues exist with tunneling mouse events. 是否应处理事件,而不让其通过其他子级进行处理,以及如何影响组合项可能具有预期鼠标行为的控件?Should you handle the event and not have it be handled by further children toward the source, and how would that affect compositing a control where the compositing pieces might have expected mouse behaviors?

路由事件信息Routed Event Information

标识符字段Identifier field MouseDownEvent
路由策略Routing strategy 冒泡Bubbling
委托Delegate MouseButtonEventHandler

适用于