WPF冒泡事件和隧道事件

 

冒泡事件: 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>

后台代码:

WPF冒泡事件和隧道事件
        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

 

WPF冒泡事件和隧道事件

 

隧道事件: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>

WPF冒泡事件和隧道事件
//先执行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

WPF冒泡事件和隧道事件

 

WPF冒泡事件和隧道事件

上一篇:Navisworks 2014 Api 简单的使用


下一篇:noip模拟36