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

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

问题描述

下面是我的XAML:

 <窗​​口x:类=WpfApplication4.MainWindow
的xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
标题= 主窗口HEIGHT =844.025WIDTH =678的MouseUp =somethingClicked>
<网格的MouseUp =somethingClicked>
< StackPanel中的MouseUp =somethingClicked保证金=0,0,10,0>
<按钮X:NAME =btnClickMeCONTENT =点击我!的Horizo​​ntalAlignment =左VerticalAlignment =评出的WIDTH =75保证金=101,22,0,0的MouseUp =somethingClicked/>
<复选框X:NAME =chkhandleCONTENT =复选框的Horizo​​ntalAlignment =左VerticalAlignment =评出的保证金=241,28,0,0RenderTransformOrigin = - 0.588,1.188/> ;
< ListBox的X:名称=lstEvents的Horizo​​ntalAlignment =左HEIGHT =604VerticalAlignment =评出的WIDTH =416保证金=29,66,0,0/>
< / StackPanel的>
< /网格和GT;





和这里是C#代码

 命名空间WpfApplication4 
{
///<总结> $ B $为MainWindow.xaml
///< b ///交互逻辑; /总结>
公共部分类主窗口:窗口
{
保护INT eventCounter = 0;

公共主窗口()
{
的InitializeComponent();
}

私人无效somethingClicked(对象发件人,RoutedEventArgs E)
{
eventCounter ++;

字符串消息=#+ eventCounter.ToString()+:\r\\\
+
发件人:+ sender.ToString()+:\ r\\\
+
来源:+ e.Source +:\r\\\
+
原文出处:+ e.OriginalSource;

lstEvents.Items.Add(消息);
e.Handled =(布尔)chkhandle.IsChecked;

如果(e.Handled)
lstEvents.Items.Add(已完成);

}
}



}



我有这个例子以下问题:
1)MouseUp事件不上点击按钮激活。
2)事件不会不泡沫了。单击窗体显示的地方:

 发件人:WpfApplication4.MainWindow:
来源:WpfApplication4.MainWindow:
原文出处:System.Windows.Controls.Border。

如果我理解正确,点击按钮时,首先应该在窗口级别(执行它现在所做的),那么网格,堆栈最后文本标签。是代码错误或者是我的观念有问题的理解?


解决方案

MouseUp事件不会触发上单击按钮。




由于第一火灾是在事件在 Button.Click ,当它工作,它与事件冲突的MouseUp 。这里从报价的:




ButtonBase自UIElement继承,一个按钮也将有机会获得所有的UIElement定义鼠标按键事件。因为按钮做了响应按键,它吞了冒泡事件(例如的MouseLeftButtonDown和的MouseDown)。您还可以通过添加处理程序隧道事件(例如的PreviewMouseLeftButtonDown和PreviewMouseDown)检测到这些低级别按钮按下事件。




尝试更换按钮标签,你会得到期望的结果:




Here is my 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>

And here is the C# Code:

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");   

    }
}

}

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.

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?

解决方案

The MouseUp event is not fired on clicking the button.

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

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

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

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