我可以对同一控件的同一个命令有多个CommandBindings? [英] Can I have multiple CommandBindings for the same Command on the same control?

查看:138
本文介绍了我可以对同一控件的同一个命令有多个CommandBindings?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UserControl添加一个CommandBinding到它的CommandBindings集合来处理一个特定的命令。后来我在一个窗口中使用这个控件,并希望添加另一个绑定到同一个控件以添加额外的行为。但问题是,当我这样做,似乎当我添加另一个CommandBinding到控件的CommandBindings集合,它替换已经为同一个命令的任何绑定。所以它似乎是一个控件只能有一个单一的CommandBinding每个控件,这是正确的吗?

I have a UserControl that adds a CommandBinding to it's CommandBindings collection to handle a specific Command. Later I use this control in a window and want to add another binding to that same control to add additional behavior. The problem though, is that when I do this it seems that when I add another CommandBinding to the CommandBindings collection of a control that it replaces any binding that was already made for the same Command. So what it seems like is that a control can only have a single CommandBinding per control, is this correct?

请参阅下面的代码示例,尝试设置两个CommandBindings

Please see the code example below which attempts to set two CommandBindings for the same Save Command.

<Window x:Class="MultipleCommandBindings.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.CommandBindings>
    <CommandBinding Command="Save"
                    Executed="CommandBinding_Executed" />
    <CommandBinding Command="Save"
                    Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<Grid>
    <Button Height="23"
            HorizontalAlignment="Right"
            Margin="0,0,25,88"
            Name="button1"
            VerticalAlignment="Bottom"
            Width="75"
            Command="Save">Button</Button>
</Grid>

编译时或运行时异常当写这个代码,但是惊讶,它没有抱怨。接下来虽然我很失望,因为我的CommandBinding_Executed处理程序只被调用一次而不是我希望的两次。

Originally I was expecting either a compile-time or runtime exception when wrote this code but was surprised that it didn't complain. Next though I was disappointed since my CommandBinding_Executed handler only gets called once instead of twice as I was hoping.

更新:一点测试看来,我的第二个CommandBinding不覆盖我的第一个,但相反,似乎即使我不是设置Handled为真在我的事件处理程序,第一个命令绑定吞没了命令。我很确定在这一点上,我的问题的解决方案是理解为什么路由命令不传播通过第一个处理程序,即使Handled没有设置为true。

Update: After a bit of testing it appears that my second CommandBinding is not overwriting my first one but instead it appears that even though I'm not setting Handled to true in my event handler that the first command binding swallows up the Command. I'm pretty sure at this point that the solution to my problem is to understand why the routed command is not propagating past the first handler even when Handled is not set to true.

更新:
我找到了这个

Update: I've found this great little tidbit of information which just confirms some of the strange behavior behind Command routing in WPF.

更新:







b一个想法如何解决这一事实,似乎只有一个有效的CommandBinding每个命令是,似乎默认的CommandBinding类暴露Executed和CanExecute作为事件,当然像所有事件可以有多个处理程序。这样的想法是使用一些不同于标准CommandBindings.Add方法的方法来向命令添加额外的处理程序。也许这可以通过控制类上的一个扩展方法,并结合一个自定义的CompositeCommandBinding类,允许我们在一个主绑定中聚合多个绑定。

Update: One thought about how to work around the fact that it appears that there can only be a single effective CommandBinding per command is that it appears that the default CommandBinding class exposes Executed and CanExecute as events which of course like all events can have multiple handlers. The idea then is to have some other way than the standard CommandBindings.Add method to add additional handlers to a command. Maybe this could be done via an extension method on the Control class and conjunction with a custom CompositeCommandBinding class which allows us to aggregate multiple bindings within one main binding.

推荐答案

到目前为止,我只能提出一个解决方法,这个问题是在逻辑树中的两个不同的级别处理命令。在下面的示例中,我在网格内处理Save命令,然后再次在Window元素内处理。

So far I've only been able to come up with a workaround for this problem which is to handle the command at two different levels in the logical tree. In the example below I handle the Save command within my grid and then also again within the Window element.

<Window x:Class="MultipleCommandBindings.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.CommandBindings>
    <CommandBinding Command="Save"
                    Executed="CommandBinding_Executed2"/>
</Window.CommandBindings>
<Grid>
    <Grid.CommandBindings>
        <CommandBinding Command="Save"
                        Executed="CommandBinding_Executed1" />
    </Grid.CommandBindings>

    <Button Height="23"
            HorizontalAlignment="Right"
            Margin="0,0,25,88"
            Name="button1"
            VerticalAlignment="Bottom"
            Width="75"
            Command="Save">Button</Button>
</Grid>

工作我的代码后面需要也手动传播的命令执行到一个新的层次。

In order to get this to work my code behind needs to also manually propagate the Command execution to the next level up.

    private void CommandBinding_Executed1(object sender, ExecutedRoutedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Executed 1!");
        var command = e.Command as RoutedUICommand;
        command.Execute(e.Parameter, this);
    }

    private void CommandBinding_Executed2(object sender, ExecutedRoutedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Executed 2!");
    }

如果有人有更好的想法如何监视命令,在树上自然传播,我很想看到它,否则我可能只是诉诸这个解决方法。

If anybody has any better ideas on how I can monitor a command still let it propagate naturally up the tree I would love to see it otherwise I might just resort to this workaround.

这篇关于我可以对同一控件的同一个命令有多个CommandBindings?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆