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. 附加的事件並沒有直接處理語法在程式碼若要附加的程式碼中的路由事件處理常式,您會使用指定的新增 * 處理常式方法。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) Framework 將根據此附加事件的呈現為兩個不同通用語言執行平台 (CLR)common language runtime (CLR)上的事件UIElementContentElement: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. 做出最適當的選擇,關於是否父項目也應被告知關於任何指定的滑鼠動作其實就是為什麼困難WPFWPFframework 選擇模型做為基礎的路由傳送的滑鼠事件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