这个问题,已成为我学习的瓶颈了,没有朋友能帮我解决<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,冒泡事件就不引发了?谢谢!!希望这次能得到答案..............

解决方案 »

  1.   

    我好像回答过了。因为在messagebox里面响应了左键事件。后面的被吃掉了。
      

  2.   

    问题是,messagebox响应左键和button1有什么关系呢?
      

  3.   

    button 的MouseDown 鼠标左键点击 只执行隧道事件(内部执行完隧道会标记为已处理)  
     右键会执行隧道和冒泡事件 (你的弹出框可能内部会标记成已处理,导致冒泡事件不会执行)
      

  4.   

    我试了下,全程的Handled都为false,为什么冒泡事件就没有触发呢?
      

  5.   

    就算是在构造函数中设置:
    button1.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(button1_MouseDown), true);仍然不引发冒泡事件,此为何因?
      

  6.   

    刚才试了一下,的确和楼主描述的现象一样,个人分析原因可能如下
    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中就创建了模态对话框同时主窗口的窗口处理函数会被阻塞,所以就解释了剩下的事件不会产生。
    不知道这样回答对你有没有帮助。
      

  7.   

    刚才试了一下,的确和楼主描述的现象一样,个人分析原因可能如下
    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。