【发布时间】:2012-04-25 16:44:43
【问题描述】:
让我们从元素树开始。最上面的元素是一个注册了ApplicationCommands.Find 命令的WPF 窗口。
一些子元素具有 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");
}
}
【问题讨论】:
标签: wpf events routed-commands