隧道事件和冒泡事件在WPF中何时有用? [英] When are tunneling and bubbling events useful in WPF?

查看:373
本文介绍了隧道事件和冒泡事件在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

许多标准控件都监听事件,例如KeyDownMouseDown等.例如-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:

了解WPF中的路由事件和命令

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

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