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