【发布时间】:2016-02-08 06:23:21
【问题描述】:
我有一个这样定义的 RoutedCommand:
public static class Commands
{
/// <summary>
/// Represents the Foo feature of the program.
/// </summary>
public static readonly RoutedCommand Foo = new RoutedCommand("Foo", typeof(Window1));
}
然后我有一个用户控件,其命令定义如下:
<UserControl.CommandBindings>
<CommandBinding Command="{x:Static local:Commands.Foo}" Executed="CommandBinding_Executed"/>
</UserControl.CommandBindings>
<UserControl.InputBindings>
<KeyBinding Command="{x:Static local:Commands.Foo}" Modifiers="Control" Key="Space"></KeyBinding>
</UserControl.InputBindings>
并像这样在代码中处理! (针对我的用户控件类)
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Executed from sub");
}
我的自定义用户控件在我的主窗口中定义:
<local:UserControl1 x:Name="myC" Grid.Row="1" DataContext="{Binding ChildName}"></local:UserControl1>
我的主窗口也有自己的方式来处理 Foo 路由命令:
CommandBindings.Add(new CommandBinding(Commands.Foo, new ExecutedRoutedEventHandler((x, y) => {
MessageBox.Show("Something Happend from main window");
})));
InputBindings.Add(new InputBinding(Commands.Foo, new KeyGesture(Key.P, ModifierKeys.Alt)));
现在,如果该命令是从我的用户控件触发的,如果 ExecutedRoutedEventArgs 的 Handled 属性设置为 false,我希望事件会冒泡到窗口,并且它是:
但只有来自用户控件的消息框会显示,我希望窗口上已处理事件的消息框会在之后显示!
谢谢
【问题讨论】:
-
您在 UC 和 MainWindow 中使用不同的输入绑定。如果按 Alt+P 会发生什么?