今天做一个wpf用户控件的时候,对Button的MouseLeftButtonDown事件实在是触发不了,所以,编写了个简单的demo来测试Button的事件的触发顺序.直接上代码:

<Button Name="btnTop" Width="300" Height="300"
            Tag="Top   "  
           
            PreviewMouseLeftButtonDown="MyRoutedEventHandler"
            PreviewMouseDown="MyRoutedEventHandler"
            PreviewMouseUp="MyRoutedEventHandler"
            PreviewMouseLeftButtonUp="MyRoutedEventHandler"
           
            MouseLeftButtonDown="MyRoutedEventHandler"
            MouseDown="MyRoutedEventHandler"
            Click="MyRoutedEventHandler"
            MouseDoubleClick="MyRoutedEventHandler"
            MouseUp="MyRoutedEventHandler"
            MouseLeftButtonUp="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));
        }

输出:

 24:05 Top    PreviewMouseLeftButtonDown
 24:05 Top    PreviewMouseDown
 24:05 Top    PreviewMouseLeftButtonUp
 24:05 Top    PreviewMouseUp
 24:05 Top    Click

感觉到,还是有很多的事件没有执行,非常的郁闷。

查看wpf的事件的相关文档,太TMD高深了,

http://www.docin.com/p-3021557.html

为了解决这个问题,修改成如下的代码:

   <Button Name="btnTop" Width="300" Height="300"
            Tag="Top   "  
           
            PreviewMouseLeftButtonDown="MyRoutedEventHandler"
            PreviewMouseDown="MyRoutedEventHandler"
            PreviewMouseUp="MyRoutedEventHandler"
            PreviewMouseLeftButtonUp="MyRoutedEventHandler"
           
            MouseLeftButtonDown="btnTop_MouseLeftButtonDown"
            MouseDown="btnTop_MouseLeftButtonDown"
            Click="MyRoutedEventHandler"
            MouseDoubleClick="MyRoutedEventHandler"
            MouseUp="btnTop_MouseLeftButtonDown"
            MouseLeftButtonUp="btnTop_MouseLeftButtonDown"
            >

  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));
            e.Handled = true;
        }

        private void btnTop_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Debug.WriteLine(string.Format("\t{0:mm:ss}\t{1}\t{2}", DateTime.Now, ((Button)sender).Tag, e.RoutedEvent.Name));
        }

 

输出:

29:20 Top    PreviewMouseLeftButtonDown
 29:20 Top    PreviewMouseLeftButtonUp

靠,更少了、、、

在改:

   <Button Name="btnTop" Width="300" Height="300"
            Tag="Top   "  
           
            PreviewMouseLeftButtonDown="MyRoutedEventHandler"
            PreviewMouseDown="MyRoutedEventHandler"
            PreviewMouseUp="MyRoutedEventHandler"
            PreviewMouseLeftButtonUp="MyRoutedEventHandler"
           
            MouseLeftButtonDown="btnTop_MouseLeftButtonDown"
            MouseDown="btnTop_MouseLeftButtonDown"
            Click="MyRoutedEventHandler"
            MouseDoubleClick="MyRoutedEventHandler"
            MouseUp="btnTop_MouseLeftButtonDown"
            MouseLeftButtonUp="btnTop_MouseLeftButtonDown"
            >

  public Window1()
        {
            InitializeComponent();
            btnTop.AddHandler(Button.MouseLeftButtonDownEvent, new RoutedEventHandler(btnTop_MouseLeftButtonDown), true);
            btnTop.AddHandler(Button.MouseDownEvent, new RoutedEventHandler(btnTop_MouseLeftButtonDown), true);
            btnTop.AddHandler(Button.MouseUpEvent, new RoutedEventHandler(btnTop_MouseLeftButtonDown), true);
            btnTop.AddHandler(Button.MouseLeftButtonUpEvent, new RoutedEventHandler(btnTop_MouseLeftButtonDown), 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));
          
        }

        private void btnTop_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine(string.Format("\t{0:mm:ss}\t{1}\t{2}", DateTime.Now, ((Button)sender).Tag, e.RoutedEvent.Name));
        }

 

输出:

 31:41 Top    PreviewMouseLeftButtonDown
 31:41 Top    PreviewMouseDown
 31:41 Top    MouseLeftButtonDown
 31:41 Top    MouseDown
 31:41 Top    PreviewMouseLeftButtonUp
 31:41 Top    PreviewMouseUp
 31:41 Top    Click
 31:41 Top    MouseLeftButtonUp
 31:41 Top    MouseUp

靠,总算成功了,

但是,似乎不爽,因为Button的MouseLeftButtonDown等事件是MouseButtonEventHandler。

在修改:

            btnTop.AddHandler(Button.MouseLeftButtonDownEvent, new MouseButtonEventHandler(btnTop_MouseLeftButtonDown), true);
            btnTop.AddHandler(Button.MouseDownEvent, new MouseButtonEventHandler(btnTop_MouseLeftButtonDown), true);
            btnTop.AddHandler(Button.MouseUpEvent, new MouseButtonEventHandler(btnTop_MouseLeftButtonDown), true);
            btnTop.AddHandler(Button.MouseLeftButtonUpEvent, new MouseButtonEventHandler(btnTop_MouseLeftButtonDown), true);

输出:

 35:04 Top    PreviewMouseLeftButtonDown
 35:04 Top    PreviewMouseDown
 35:05 Top    MouseLeftButtonDown
 35:05 Top    MouseDown
 35:05 Top    PreviewMouseLeftButtonUp
 35:05 Top    PreviewMouseUp
 35:05 Top    Click
 35:05 Top    MouseLeftButtonUp
 35:05 Top    MouseUp

代码奉上,但是,没任何讲解。

posted on 2009-05-31 22:45  颜昌钢  阅读(10407)  评论(1编辑  收藏  举报