Wpf事件不冒泡 [英] Wpf event not bubbling

查看:121
本文介绍了Wpf事件不冒泡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的XAML:

<Window x:Class="WpfApplication4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="844.025" Width="678"  MouseUp="somethingClicked">
<Grid MouseUp="somethingClicked">
    <StackPanel MouseUp="somethingClicked" Margin="0,0,10,0">
    <Button x:Name="btnClickMe" Content="Click Me!" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="101,22,0,0" MouseUp="somethingClicked"/>
        <CheckBox x:Name="chkhandle" Content="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="241,28,0,0" RenderTransformOrigin="-0.588,1.188"/>
        <ListBox x:Name="lstEvents" HorizontalAlignment="Left" Height="604" VerticalAlignment="Top" Width="416" Margin="29,66,0,0"/>
    </StackPanel>
</Grid>

这里是C#代码:

namespace WpfApplication4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    protected int eventCounter = 0;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void somethingClicked(object sender, RoutedEventArgs e)
    {
        eventCounter++;

        String message = "#" + eventCounter.ToString() + ":\r\n" +
            " Sender: " + sender.ToString() + ":\r\n" +
            " Source: " + e.Source + ":\r\n" +
            " Original Source: " + e.OriginalSource;

        lstEvents.Items.Add(message);
        e.Handled = (bool) chkhandle.IsChecked;

        if (e.Handled)
            lstEvents.Items.Add("Completed");   

    }
}

}

我在此示例中遇到以下问题:
1)单击按钮时不会触发MouseUp事件。
2)事件不会起泡。点击窗体上的某个地方显示:

I have the following issues with this example: 1)The MouseUp event is not fired on clicking the button. 2)The event doesn't bubble up. Clicking somewhere on the form displays:

Sender:WpfApplication4.MainWindow:
Source:WpfApplication4.MainWindow:
Original Source: System.Windows.Controls.Border.

如果我明白了,当按钮被点击时,首先应该在Window级别执行现在),然后网格,然后堆栈,最后文本标签。代码错误还是我对概念的理解错误?

If I understand rightly, when button is clicked, first it should be executed at Window level (which it does now), then Grid, then stack and finally text label. Is the code wrong or is my understanding of the concept faulty?

推荐答案


MouseUp事件在点击按钮时不会触发。

因为第一次火灾是 Button.Click ,它的工作原理与事件 MouseUp 发生冲突。报价从 here

Because the first fires is an event at the Button.Click, and when it works, it conflicts with the event MouseUp. Quote from here:


ButtonBase从UIElement继承,Button还可以访问为UIElement定义的所有鼠标按钮事件。因为按钮响应按钮按钮,它会吞没冒泡事件(例如MouseLeftButtonDown和MouseDown)。您仍然可以通过添加隧道事件的处理程序(例如PreviewMouseLeftButtonDown和PreviewMouseDown)来检测这些较低级别的按钮按钮事件。

ButtonBase inherits from UIElement, a Button will also have access to all of the mouse button events defined for UIElement. Because the Button does something in response to button presses, it swallows the bubbling events (e.g. MouseLeftButtonDown and MouseDown). You can still detect these lower level button press events by adding handlers for the tunneling events (e.g. PreviewMouseLeftButtonDown and PreviewMouseDown).

尝试将按钮替换为 / code>,您将获得所需的结果:

Try to replace the Button on Label, and you'll get the desired result:

这篇关于Wpf事件不冒泡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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