让我们从元素树开始。最上面的元素是一个WPF窗口,它已经注册了ApplicationCommands.Find
命令。 某些子元素具有KeyBinding,并且手势键ENTER指向此命令。这没关系,如果有人按下ENTER键,命令将被执行。 其中我有一个自定义控件,用于搜索和选择一些元素的弹出窗口。在这个地方,虽然, 我不想让自定义控件之外的关键事件冒泡。但我设置e.handled = true
为 KeyDown += new KeyEventHandler (..)
,这应该是冒泡路由事件,该自定义控件内的所有控件(文本框等)都将忽略任何类型的输入。 但我只是想停止在CustomControl的级别冒泡,说:从叶子到最顶层的控制父级,而不是进一步! 这是为什么?我不过滤PreviewKeyDown
,所以事件必须传播到叶,然后它应该回到父,但它不。WPF防止事件在控件之外冒泡
谢谢
编辑:示例代码:
<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");
}
}
不完全。我想停止传播事件到自定义控件的主窗口..但是,谢谢 – 2012-04-17 07:07:40
这就是我所理解的。我认为一些代码可能会有帮助。 – Natxo 2012-04-17 08:26:41
嗨PaN1C,我编辑我的答案后,您的示例代码 – Natxo 2012-04-18 08:51:07