顺便说一下,我正在阅读有关 Routed Event 的内容,这是一个非常酷的概念,我了解事件冒泡如何从子元素到根元素工作,但我不确定事件隧道如何工作。我创建了一个小例子,我在网格中放置了一个按钮并将 previewKeyup 事件附加到所有但在按下键事件之后在根级别处理而不是隧道到子级。
<Window x:Class="ExplorerContentControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Content Control" Height="400" Width="400" FontFamily="Calibri" FontSize="13" FontWeight="Bold" Button.Click ="Window_Click" PreviewKeyUp="Window_PreviewKeyUp" >
<Grid Button.Click ="Grid_Click" PreviewKeyUp="Grid_PreviewKeyUp">
<Button Name="btnClickedMe" Click="btnClickedMe_Click" Margin="3" HorizontalAlignment="Left" Height="25" Width="80" Content="Click Me" PreviewKeyUp="btnClickedMe_PreviewKeyUp" />
</Grid>
</Window>
在后面的代码中
private void btnClickedMe_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("From Btn Clicked");
}
private void Grid_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("at Grid Location...");
}
private void Window_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("at Windows Location ...");
}
private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
{
MessageBox.Show("PreviewKeyUp From Windows...");
}
private void Grid_PreviewKeyUp(object sender, KeyEventArgs e)
{
MessageBox.Show("PreviewKeyUp From Grid...");
}
private void btnClickedMe_PreviewKeyUp(object sender, KeyEventArgs e)
{
MessageBox.Show("PreviewKeyUp From Button...");
}
虽然在事件冒泡中出现正确的消息框buttonClick
,WindowClick
但在PreviewKeyup
. 有人可以向我解释一下事件隧道在我的示例中是如何工作的吗?