让我们从元素树开始。最上面的元素是一个 WPF 窗口,它已经注册了ApplicationCommands.Find
命令。某些子元素具有 KeyBinding,手势键 ENTER 指向此命令。没关系,如果有人按 ENTER,则将执行该命令。其中我有一个自定义控件,带有一个用于搜索和选择某些元素的弹出窗口。不过,在这个地方,我不想让自定义控件之外的键事件冒泡。但是我设置e.handled = true
了
KeyDown += new KeyEventHandler (..)
,这应该是一个冒泡路由事件,该自定义控件内的所有控件(文本框等)将忽略任何类型的输入。但我只想在 CustomControl 级别停止冒泡,说:从叶子到最顶层的控件父级,而不是更远!这是为什么?我没有过滤PreviewKeyDown
,所以事件必须传播到叶子,然后它应该回到父节点,但事实并非如此。
谢谢
编辑:示例代码:
<UserControl x:Class="WpfApplication5.TestUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<StackPanel Orientation="Vertical">
<TextBox />
<TextBox />
<TextBox />
</StackPanel>
</UserControl>
/*
* I want to stop bubbling at this level. Let us say if we click 'enter' on the level of the TextBox leaf,
* it should propagate until the TestUserControl is reached and then stop.
*/
public partial class TestUserControl : UserControl
{
public TestUserControl()
{
InitializeComponent();
}
}
<Window x:Class="WpfApplication5.Window6"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication5"
Title="Window6" Height="300" Width="300">
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Find" Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Gesture="Enter" Command="ApplicationCommands.Find" />
</Window.InputBindings>
<Grid>
<local:TestUserControl />
</Grid>
</Window>
public partial class Window6 : Window
{
public Window6()
{
InitializeComponent();
}
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Executed");
}
}