学习WPF以来,一直以为隧道路由事件是由触发控件向其子控件传递的,见天才知道,原来隧道路由事件是从窗口想当前元素传递的!
汗颜啊!
贴上一段示例代码来说明一下:
<Grid> <Border Height="50" Width="250" BorderBrush="Gray" BorderThickness="1" > <StackPanel Background="LightGray" Orientation="Horizontal" PreviewMouseLeftButtonUp="StackPanel_PreviewMouseLeftButtonUp" MouseUp="StackPanel_MouseUp"> <TextBlock Name="YesTB" Width="50" PreviewMouseLeftButtonUp="YesTB_PreviewMouseLeftButtonUp" MouseUp="YesTB_MouseUp" Background="Blue" >Yes</TextBlock> </StackPanel> </Border> </Grid>
后台代码:
private void YesTB_MouseUp(object sender, MouseButtonEventArgs e) { //MessageBox.Show("button"); e.Handled = true; } private void StackPanel_MouseUp(object sender, MouseButtonEventArgs e) { //MessageBox.Show("Panel"); } private void YesTB_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { MessageBox.Show("buttonPreview"); } private void StackPanel_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { MessageBox.Show("PanelPreview"); }
程序运行效果如图:
当点击上图中的蓝色区域,首先会弹出“PanelPreivew”对话框,之后才弹出“buttonPreview”对话框。
学习了!