Mouse.MouseDown Mouse.MouseDown Mouse.MouseDown Attached Event

定义

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

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

注解

若要确定鼠标按下了哪个按钮, ChangedButton请在传递MouseButtonEventArgs到处理程序的中检查属性。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.

MouseLeftButtonDown MouseRightButtonDown UIElement 公共语言运行时 (CLR)common language runtime (CLR)此框架通过在和ContentElement上将其作为两个不同的事件进行呈现, 在此附加事件上生成: 和。 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF)The 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

适用于