这个问题,已成为我学习的瓶颈了,没有朋友能帮我解决<Window x:Class="WPF熊俊.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="308" Width="552" >
<Grid Height="202" Name="grid1" Width="325" MouseDown ="button1_MouseDown" PreviewMouseDown="button1_PreviewMouseDown">
<Button Height="23" Name="button1" Width="75" MouseDown="button1_MouseDown" PreviewMouseDown="button1_PreviewMouseDown">button1</Button>
</Grid>
</Window>
后台:public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_MouseDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("MouseDown");
}
private void button1_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("PreviewMouseDown");
}
}
给Grid和Button都注册了冒泡事件和隧道事件。当用鼠标右键(注意,是右键)点击button的时候,为什么只弹出了2次"PreviewMouseDown",而没有弹出"MouseDown",这就是说为什么冒泡事件没有触发呢?
困惑的是,如果不用MessageBox.Show,而是用一个变量来记录冒泡事件和隧道事件触发的次数,结果却正好是4次,那说明隧道事件和冒泡事件都引发了。为什么用MessageBox.Show,冒泡事件就不引发了?谢谢!!希望这次能得到答案..............
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="308" Width="552" >
<Grid Height="202" Name="grid1" Width="325" MouseDown ="button1_MouseDown" PreviewMouseDown="button1_PreviewMouseDown">
<Button Height="23" Name="button1" Width="75" MouseDown="button1_MouseDown" PreviewMouseDown="button1_PreviewMouseDown">button1</Button>
</Grid>
</Window>
后台:public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_MouseDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("MouseDown");
}
private void button1_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("PreviewMouseDown");
}
}
给Grid和Button都注册了冒泡事件和隧道事件。当用鼠标右键(注意,是右键)点击button的时候,为什么只弹出了2次"PreviewMouseDown",而没有弹出"MouseDown",这就是说为什么冒泡事件没有触发呢?
困惑的是,如果不用MessageBox.Show,而是用一个变量来记录冒泡事件和隧道事件触发的次数,结果却正好是4次,那说明隧道事件和冒泡事件都引发了。为什么用MessageBox.Show,冒泡事件就不引发了?谢谢!!希望这次能得到答案..............
解决方案 »
- 求助一个问题?
- SQL 数据库里的 null 怎么查找?
- C# soket 网络通讯 为何只能发送一次就没法继续了?
- 爬虫可以登陆下一个文件,但是不能连续下文件,大家来看看什么原因?
- 本机不安装OFFICE的情况下,在应用程序上实现OFFICE相关的WORD EXCEL导入导出操作
- 请问swich case语句 如果我想CASE两个字符串怎么做?
- 电子尺,套打时特别有用
- postgres问题(invalid byte sequence for encoding "UTF8")
- C#利用webBrowser怎么去掉网页中不想显示的部分,高手请指教!!
- 水晶报表问题
- 求助一个C#图像处理
- C#语言环境中利用GDI+实现像WINDOWS中一样的矩形虚线框选取中的问题
右键会执行隧道和冒泡事件 (你的弹出框可能内部会标记成已处理,导致冒泡事件不会执行)
button1.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(button1_MouseDown), true);仍然不引发冒泡事件,此为何因?
private void MyRoutedEventHandler(object sender, RoutedEventArgs e)
{
Debug.WriteLine(string.Format("\t{0:mm:ss}\t{1}\t{2}", DateTime.Now, ((Button)sender).Tag, e.RoutedEvent.Name));
//MessageBox.Show(string.Format("\t{0:mm:ss}\t{1}\t{2}", DateTime.Now, ((Button)sender).Tag, e.RoutedEvent.Name));
}
鼠标完成一个单击按钮操作,事件的发生顺序如下:
08:37 Bottom PreviewMouseLeftButtonDown
08:37 Bottom PreviewMouseDown
08:37 Bottom MouseLeftButtonDown
08:37 Bottom MouseDown
08:37 Bottom PreviewMouseLeftButtonUp
08:37 Bottom PreviewMouseUp
08:37 Bottom Click
08:37 Bottom Click
08:37 Bottom MouseLeftButtonUp
08:37 Bottom MouseUp
如果屏蔽掉Debug.WriteLine(...); 改用MessageBox.Show(...);你会发现弹出PreviewMouseDown的对话框后就没有了。WPF终归还是要对Windows消息循环进行封装,MessageBox是一个模态的对话框,有其自己的消息循环,调用模态对话框的窗口处理函数会被阻塞。
当鼠标左键按下发生时,首先产生PreviewMouseLeftButtonDown和PreviewMouseDown,其处理函数MyRoutedEventHandler中就创建了模态对话框同时主窗口的窗口处理函数会被阻塞,所以就解释了剩下的事件不会产生。
不知道这样回答对你有没有帮助。
private void MyRoutedEventHandler(object sender, RoutedEventArgs e)
{
Debug.WriteLine(string.Format("\t{0:mm:ss}\t{1}\t{2}", DateTime.Now, ((Button)sender).Tag, e.RoutedEvent.Name));
//MessageBox.Show(string.Format("\t{0:mm:ss}\t{1}\t{2}", DateTime.Now, ((Button)sender).Tag, e.RoutedEvent.Name));
}
鼠标完成一个单击按钮操作,事件的发生顺序如下:
08:37 Bottom PreviewMouseLeftButtonDown
08:37 Bottom PreviewMouseDown
08:37 Bottom MouseLeftButtonDown
08:37 Bottom MouseDown
08:37 Bottom PreviewMouseLeftButtonUp
08:37 Bottom PreviewMouseUp
08:37 Bottom Click
08:37 Bottom Click
08:37 Bottom MouseLeftButtonUp
08:37 Bottom MouseUp
如果屏蔽掉Debug.WriteLine(...); 改用MessageBox.Show(...);你会发现弹出PreviewMouseDown的对话框后就没有了。WPF终归还是要对Windows消息循环进行封装,MessageBox是一个模态的对话框,有其自己的消息循环,调用模态对话框的窗口处理函数会被阻塞。
当鼠标左键按下发生时,首先产生PreviewMouseLeftButtonDown和PreviewMouseDown,其处理函数MyRoutedEventHandler中就创建了模态对话框同时主窗口的窗口处理函数会被阻塞,所以就解释了剩下的事件不会产生。
不知道这样回答对你有没有帮助。
只有先收录了。到处都查不到资料,而且Handled也是ture。