隧道事件和冒泡事件在WPF中何时有用? [英] When are tunneling and bubbling events useful in WPF?
问题描述
我了解起泡和隧穿的工作原理.但是,我对使用它们感到困惑. 这是原因:
I understand how bubbling and tunneling works. However, i'm confused about using them. Here is why:
我想处理鼠标单击事件.要冒泡,有MouseDown
,要挖通它,有PreviewMouseDown
.但是,MouseDown
不一定意味着用户单击了控件.可能是用户按下了按钮并从其移开以取消单击.
如果不单击该按钮,我不想更改任何内容.
I want to handle a mouse click event. To bubble it, there is MouseDown
and, to tunnel it, there is PreviewMouseDown
. However, MouseDown
doesn't necessarily mean the user clicked the control. May be the user pressed the button and moved away from it to cancel the click.
I wouldn't want to change anything if the button is not being clicked.
所以我的问题是,冒泡/挖洞策略有何用处?
So my question is, how are the Bubbling/Tunneling strategies useful?
推荐答案
如果事件在RoutedEventArgs
中列出,则为路由事件.路由事件支持Bubble,Tunnel或Direct的RoutingStrategy.让我们看一下Button.Click
的事件处理程序:
If the event is listed RoutedEventArgs
, then it's routed event. Routed events support a RoutingStrategy of Bubble, Tunnel, or Direct. Let's take a look at the event handler of Button.Click
:
private void Grid_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button Test clicked!");
}
那里指定了RoutedEventArgs
,所以它是路由事件.由于未在名称中指定预览,因此此Bubble事件.可以通过以下方式证明这一点:
There specified RoutedEventArgs
, so it's routed event. Because the preview were not specified in the name, therefore this Bubble event. This can be demonstrated in the following way:
<Grid ButtonBase.Click="Grid_Click">
<Button Name="TestButton" Width="100" Height="30" Content="Test" />
</Grid>
当您单击TestButton
时,事件将上升到Grid
上方,并显示一条消息:
When you click on the TestButton
, the event is to rise above the Grid
, and displays a message:
单击按钮测试!
Button Test clicked!
Usefulness of Bubbling/Tunneling strategies
Usefulness of Bubbling/Tunneling strategies
Tunneling
许多标准控件都监听事件,例如KeyDown
,MouseDown
等.例如-DataGrid
控件.我想通过按Enter键将该功能称为添加记录.但是DataGrid
已经具有KeyDown
事件,因此不会引发该事件.因此,您必须在Tunnel事件中执行逻辑-PreviewKeyDown
,它将在KeyDown
事件之前起作用. RichTextBoxControl
也是如此.
Many of the standard controls listen to events, such as KeyDown
, MouseDown
, etc. For example -DataGrid
control. I want by pressing the enter key the function was called adding a record. But DataGrid
already has KeyDown
event, so the event is not raised. So you have to do your logic in the Tunnel event - PreviewKeyDown
, it will work before the KeyDown
event. The same applies to RichTextBoxControl
.
Bubbling
有时,您需要用于特定事件的全局处理程序,因此它适用于VisualTree中的所有控件.自然,直接事件是您做不到的.因此,舞台上出现了冒泡事件.
Sometimes, you need a global handler for a specific event, so it worked for all controls in VisualTree. Naturally, the a direct event you can not do it. Hence on the stage comes Bubbling event.
另一个原因是WPF的意识形态.此Button
可以包含任何内容:Image
,另一个Button
等:
Another reason is the ideology of the WPF. This Button
can contain anything: Image
, another Button
, etc:
用户可以单击Button
中的TextBlock/Image
.我们如何知道该点击是在Button
中?没错,借助冒泡事件.
The user can click on the TextBlock/Image
in the Button
. How do we know that the click was in Button
? That's right, with the help of Bubbling event.
有关更多信息,请参见:
For more information, please see:
Edit
我稍微更改了Click
处理程序:
I changed little bit a Click
handler:
private void Grid_Click(object sender, RoutedEventArgs e)
{
String message = "#" + eventCounter.ToString() + ":\r\n" +
" Sender: " + sender.ToString() + ":\r\n" +
" Source: " + e.Source + ":\r\n" +
" Original Source: " + e.OriginalSource;
lstEvents.Items.Add(message);
}
点击Button
的结果:
这篇关于隧道事件和冒泡事件在WPF中何时有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!