PreviewMouseLeftButtonDown 和 MouseLeftButtonDown WPF 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24955065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between PreviewMouseLeftButtonDown and MouseLeftButtonDown WPF
提问by gkb
While learning WPF (I am new to this), I created a simple Window and put one TextBox for entering username. I put some Text value initially in this TextBox (say Username). I wanted this text to disappear as soon as MouseLeftButtonDown is fired. Below is my xaml and C# code-
在学习 WPF 时(我是新手),我创建了一个简单的 Window 并放置了一个用于输入用户名的 TextBox。我最初在这个 TextBox 中放置了一些 Text 值(比如用户名)。我希望这个文本在 MouseLeftButtonDown 被触发后立即消失。下面是我的 xaml 和 C# 代码-
<TextBox Name="usernameTextBox" Background="Transparent" PreviewMouseLeftButtonDown="usernameTextBox_PreviewMouseLeftButtonDown" HorizontalAlignment="Left" Height="23" Margin="10,103,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="166" Text="Username" />
C# code
C#代码
private void usernameTextBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (usernameTextBox.Text.ToLower() == "username")
usernameTextBox.Text = "";
}
This however, didn't work. After some search, I came across this SO question. And PreviewMouseLeftButtonDown event worked as expected.
然而,这并没有奏效。经过一番搜索,我遇到了这个 SO question。并且 PreviewMouseLeftButtonDown 事件按预期工作。
So my question is, what is the difference between these two events and how would I know when to use one and when to use the other?
所以我的问题是,这两个事件之间有什么区别,我怎么知道什么时候使用一个,什么时候使用另一个?
Thanks!
谢谢!
回答by Sheridan
Other Microsoft technologies like Windows Forms have standard CLR events. These are described as:
其他 Microsoft 技术(如 Windows 窗体)具有标准的 CLR 事件。这些描述如下:
Only the source element itself is given the opportunity to invoke handlers in response. This is analogous to the "routing" that Windows Forms uses for events.
只有源元素本身有机会调用处理程序作为响应。这类似于 Windows 窗体用于事件的“路由”。
For WPF, Microsoft have introduced RoutedEvents, with three separate Routing Strategies As always, Microsoft has the best explanations of these different strategies (from the linked page):
对于 WPF,Microsoft 引入了RoutedEvents,具有三个单独的路由策略 一如既往,Microsoft 对这些不同的策略有最好的解释(来自链接页面):
? Bubbling: Event handlers on the event source are invoked. The routed event then routes to successive parent elements until reaching the element tree root. Most routed events use the bubbling routing strategy. Bubbling routed events are generally used to report input or state changes from distinct controls or other UI elements.
? Direct: Only the source element itself is given the opportunity to invoke handlers in response. This is analogous to the "routing" that Windows Forms uses for events. However, unlike a standard CLR event, direct routed events support class handling (class handling is explained in an upcoming section) and can be used by EventSetter and EventTrigger.
? Tunneling: Initially, event handlers at the element tree root are invoked. The routed event then travels a route through successive child elements along the route, towards the node element that is the routed event source (the element that raised the routed event). Tunneling routed events are often used or handled as part of the compositing for a control, such that events from composite parts can be deliberately suppressed or replaced by events that are specific to the complete control. Input events provided in WPF often come implemented as a tunneling/bubbling pair. Tunneling events are also sometimes referred to as Preview events, because of a naming convention that is used for the pairs.
? 冒泡:调用事件源上的事件处理程序。路由事件然后路由到连续的父元素,直到到达元素树根。大多数路由事件使用冒泡路由策略。冒泡路由事件通常用于报告来自不同控件或其他 UI 元素的输入或状态更改。
? Direct:只有源元素本身有机会调用处理程序作为响应。这类似于 Windows 窗体用于事件的“路由”。但是,与标准 CLR 事件不同,直接路由事件支持类处理(类处理将在接下来的部分中解释)并且可由 EventSetter 和 EventTrigger 使用。
? 隧道:最初,元素树根处的事件处理程序被调用。然后,路由事件沿路由通过连续的子元素,到达路由事件源(引发路由事件的元素)的节点元素。隧道路由事件通常作为控件合成的一部分使用或处理,这样来自合成部件的事件可以被故意抑制或替换为特定于整个控件的事件。WPF 中提供的输入事件通常作为隧道/冒泡对实现。由于对使用的命名约定,隧道事件有时也称为预览事件。
In simplest terms though, the Tunnelingevents, always with names starting with Preview, occur beforeBubblingevents and so are preferable to handle. The actual derived EventArgsobject that a RoutedEventuses is shared between both the Tunnelingand the relating Bubblingevents. If an event has a related Tunnelingevent, you can be certain that an attached handler will be called, whereas some controls set the Tunnelingevent as Handled, so the relating Bubblingevent is never called.
然而,用最简单的术语来说,Tunneling名称总是以 开头的事件Preview发生在Bubbling事件之前,因此更适合处理。EventArgsaRoutedEvent使用的实际派生对象在Tunneling和相关Bubbling事件之间共享。如果事件具有相关Tunneling事件,则可以确定将调用附加的处理程序,而某些控件将Tunneling事件设置为Handled,因此Bubbling永远不会调用相关事件。
Please see the linked page for the full details about Routed Events.
有关路由事件的完整详细信息,请参阅链接页面。
回答by Sajeetharan
These are all Routed Events.PreviewMouseLeftButtonDownand MouseLeftButtonDownare a pair of routed events used to notify elements in the visual tree that the user has depressed the left mouse button
这些都是路由事件。PreviewMouseLeftButtonDown和MouseLeftButtonDown是一对路由事件,用于通知可视化树中的元素用户按下了鼠标左键
Check here:
检查这里:
回答by Ugur
A typical WPF application contains many elements. Whether created in code or declared in XAML, these elements exist in an element tree relationship to each other. The event route can travel in one of two directions depending on the event definition, but generally the route travels from the source element and then "bubbles" upward through the element tree until it reaches the element tree root (typically a page or a window)
典型的 WPF 应用程序包含许多元素。无论是在代码中创建还是在 XAML 中声明,这些元素都存在于彼此的元素树关系中。事件路由可以根据事件定义沿两个方向之一行进,但通常路由从源元素开始,然后向上“冒泡”穿过元素树,直到到达元素树根(通常是页面或窗口)
MouseLeftButtonDown is a bubbles type event.
More detail: http://msdn.microsoft.com/en-us/library/ms742806.aspx
更多详细信息:http: //msdn.microsoft.com/en-us/library/ms742806.aspx
Preview events, also known as tunneling events, are routed events where the direction of the route travels from the application root towards the element that raised the event.
预览事件,也称为隧道事件,是路由事件,其中路由的方向从应用程序根向引发事件的元素行进。
More in http://msdn.microsoft.com/en-us/library/ms752279.aspx
回答by Stígandr
The PreviewMouseLeftButtonDown routed event is called before the MouseLeftButtonDown routed event. You may forinstance cancel the whole event if you want to.
PreviewMouseLeftButtonDown 路由事件在 MouseLeftButtonDown 路由事件之前调用。例如,如果您愿意,您可以取消整个活动。

