我有一个这样定义的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>
并且像这样用代码处理! (针对我的usercontrol类)
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)));
现在,如果从ExecutedRouteArgsArgs的Handled属性设置为false,并且它是从我的用户控件中触发的命令,则该事件会冒泡到窗口,并且它是:
但是只有用户控件中的消息框会显示,我希望窗口上已处理事件的消息框会在之后显示!
谢谢
CommandBinding.Executed
事件的处理程序由CommandBinding.OnExecuted
方法调用,该方法在处理程序完成后立即将RoutedEventArgs.Handled
设置为true
。该方法是内部方法,而不是虚拟方法,因此您对此行为无能为力。
如果您真的想让父级处理该命令,则可以从处理程序中调用它。这将不是同一命令,但是它将导致主窗口上的处理程序触发。
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Executed from sub");
var rc = e.Command as RoutedCommand;
var parentInput = FindParentInput();
if (parentInput != null && rc != null)
{
rc.Execute(null, parentInput);
}
}
private IInputElement FindParentInput()
{
DependencyObject element = this;
while (element != null)
{
DependencyObject parent = VisualTreeHelper.GetParent(element);
var input = parent as IInputElement;
if (input != null)
return input;
element = parent;
}
return null;
}
注意,如果您尝试将null
用作第二个参数,它将再次将新命令发送到同一UserControl的this
。这就是为什么我包括了FindParentInput
方法,该方法查找实现“ IInputElement”接口的最接近的父级。
[您可以做得更简单,似乎所有UIElement都实现了IInputElement,因此您应该可以路由到直接父级(在我的简单应用程序上进行了尝试,并且可以从我的自定义控件返回主窗口):
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
RoutedCommand c = e.Command as RoutedCommand;
IInputElement parent = this.Parent as IInputElement;
c.Execute(e.Parameter, parent);
}