码迷,mamicode.com
首页 > Windows程序 > 详细

WPF冒泡事件和隧道事件

时间:2014-11-25 22:53:17      阅读:972      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   on   

 

冒泡事件: MouseMove, 触发控件事件的顺序:从上层到底层

隧道事件: PreviewMouseMove,触发控件事件的顺序:从底层到上层

 

冒泡事件: MouseMove

<Border x:Name="greenBorder" Background="Green" Width="300" HorizontalAlignment="Right" MouseMove="greenBorder_MouseMove">
      <Border x:Name="redBorder" Background="Red" Width="120" Height="120" MouseMove="redBorder_MouseMove"></Border>
</Border>

后台代码:

bubuko.com,布布扣
        private void LowerBorder_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                text1.Text += "Lower Border x:" + e.GetPosition(this).X + "     y:" + e.GetPosition(this).Y + "\r\n";
            }
        }

        private void TopBorder_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                text1.Text += "Top Border x:" + e.GetPosition(this).X + "     y:" + e.GetPosition(this).Y + "\r\n";

                //设置Handled=true, 阻止执行LowerBorder_MouseMove事件
                e.Handled = true;
            }
        }
View Code

 

bubuko.com,布布扣

 

隧道事件:PreviewMouseMove

<Border x:Name="LowerBorder" Background="Green" Width="300" HorizontalAlignment="Right" PreviewMouseMove="LowerBorder_PreviewMouseMove">
      <Border x:Name="TopBorder" Background="Red" Width="120" Height="120" PreviewMouseMove="TopBorder_PreviewMouseMove"></Border>
</Border>

bubuko.com,布布扣
//先执行LowerBorder事件
        private void LowerBorder_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                text1.Text += "Lower Border x:" + e.GetPosition(this).X + "     y:" + e.GetPosition(this).Y + "\r\n";
            }
        }

        //后执行TopBorder事件
        private void TopBorder_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                text1.Text += "Top Border x:" + e.GetPosition(this).X + "     y:" + e.GetPosition(this).Y + "\r\n";
            }
        }
View Code

bubuko.com,布布扣

 

WPF冒泡事件和隧道事件

标签:style   blog   http   io   ar   color   os   sp   on   

原文地址:http://www.cnblogs.com/wangbc/p/4121594.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!