首页 > 代码库 > WPF冒泡事件和隧道事件
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>
后台代码:
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; } }
隧道事件: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>
//先执行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"; } }
WPF冒泡事件和隧道事件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。