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 UIElement 通用語言執行平台 (CLR)common language runtime (CLR) MouseRightButtonDown 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並選擇可讓處理常式接聽事件的簽章選項 (即使已在路由事件資料中標示為已處理), 在控制項 cti 上註冊處理常式。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 - 委派Delegate MouseButtonEventHandler

適用於