PreviewMouseLeftButtonDown和MouseLeftButtonDown WPF之间的区别 [英] Difference between PreviewMouseLeftButtonDown and MouseLeftButtonDown WPF

查看:2600
本文介绍了PreviewMouseLeftButtonDown和MouseLeftButtonDown WPF之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习WPF的同时,我创建了一个简单的窗口并放入一个TextBox输入用户名。我最初在此TextBox中放置一些Text值(如用户名)。一旦MouseLeftButtonDown被触发,我希望这个文本消失。以下是我的xaml和C#代码 -

 < TextBox Name =usernameTextBoxBackground =TransparentPreviewMouseLeftButtonDown =usernameTextBox_PreviewMouseLeftButtonDown Horizo​​ntalAlignment =LeftHeight =23Margin =10,103,0,0TextWrapping =WrapVerticalAlignment =TopWidth =166Text =Username/> 

C#代码

 code> private void usernameTextBox_MouseLeftButtonDown(object sender,MouseButtonEventArgs e)
{
if(usernameTextBox.Text.ToLower()==username)
usernameTextBox.Text =;
}

然而,这不起作用。经过搜索之后,我发现了这个SO问题
和PreviewMouseLeftButtonDown事件按预期工作。



所以我的问题是,这两个事件有什么区别,我将如何知道何时使用一个使用另一个?



谢谢!

解决方案

其他Microsoft技术像Windows Forms具有标准的CLR事件。这些描述如下:


只有源元素本身有机会调用处理程序作为响应。这类似于Windows窗体用于事件的路由。对于WPF,Microsoft已经引入了 RoutedEvent ,具有三个独立的路由策略与往常一样,Microsoft有最好的解释这些不同的策略(从链接的页面):


冒泡:事件源上的事件处理程序调用。然后,路由事件将路由到连续的父元素,直到到达元素树根。大多数路由事件使用冒泡路由策略。冒泡路由事件通常用于从不同的控件或其他UI元素报告输入或状态更改。



直接:只有源元素本身有机会援引处理程序作出回应。这类似于Windows窗体用于事件的路由。然而,与标准CLR事件不同,直接路由事件支持类处理(类处理在即将到来的部分中解释),可由EventSetter和EventTrigger使用。



Tunneling :最初,调用元素树根处的事件处理程序。然后,路由事件沿着路由通过连续的子元素,路由到路由事件源(引发路由事件的元素)的节点元素。隧道路由事件通常作为控件合成的一部分使用或处理,从而复合部件的事件可以故意抑制或由完整控件特有的事件替代。 WPF提供的输入事件通常作为隧道/冒泡对实现。隧道事件有时也称为预览事件,因为用于配对的命名约定。


简单来说, , c $ c>事件,总是以预览开头,以 $ c>冒泡事件,因此更适合处理。 RoutedEvent 使用的实际派生 EventArgs 对象在 Tunneling 和相关的冒泡事件。如果一个事件有一个相关的 Tunneling 事件,你可以确定一个附加的处理程序将被调用,而一些控件设置 Tunneling 处理,所以相关的 Bubbling 事件永远不会被调用。



有关路由事件的详细信息,请参阅链接页面。


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-

<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

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 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!

解决方案

Other Microsoft technologies like Windows Forms have standard CLR events. These are described as:

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.

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):

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.

In simplest terms though, the Tunneling events, always with names starting with Preview, occur before Bubbling events and so are preferable to handle. The actual derived EventArgs object that a RoutedEvent uses is shared between both the Tunneling and the relating Bubbling events. If an event has a related Tunneling event, you can be certain that an attached handler will be called, whereas some controls set the Tunneling event as Handled, so the relating Bubbling event is never called.

Please see the linked page for the full details about Routed Events.

这篇关于PreviewMouseLeftButtonDown和MouseLeftButtonDown WPF之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆