jpi*_*son 9 wpf command commandbinding
我有一个UserControl,它将CommandBinding添加到它的CommandBindings集合来处理特定的Command.后来我在一个窗口中使用此控件,并希望向该控件添加另一个绑定以添加其他行为.但问题是,当我这样做时,似乎当我将另一个CommandBinding添加到控件的CommandBindings集合时,它取代了已经为同一个Command创建的任何绑定.那么看起来控件每个控件只能有一个CommandBinding,这是正确的吗?
请参阅下面的代码示例,该示例尝试为同一个Save Command设置两个CommandBindings.
<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>
Run Code Online (Sandbox Code Playgroud)
最初我在编写此代码时期望编译时或运行时异常,但对它没有抱怨感到惊讶.接下来虽然我很失望,因为我的CommandBinding_Executed处理程序只被调用一次而不是我希望的两次.
更新: 经过一些测试后,看来我的第二个CommandBinding没有覆盖我的第一个,但是看起来即使我没有在我的事件处理程序中将Handled设置为true,第一个命令绑定吞下了Command.我很确定在这一点上我的问题的解决方案是理解为什么路由命令没有传播通过第一个处理程序,即使Handled未设置为true.
更新: 我发现这个很好的小信息只是证实了WPF中Command路由背后的一些奇怪行为.
更新: 考虑如何解决以下事实:每个命令似乎只有一个有效的CommandBinding,默认的CommandBinding类似乎将Executed和CanExecute暴露为事件,当然所有事件都可以有多个处理程序.然后,我们的想法是使用标准CommandBindings.Add方法之外的其他方法向命令添加其他处理程序.也许这可以通过Control类上的扩展方法完成,并与自定义CompositeCommandBinding类结合使用,该类允许我们在一个主绑定中聚合多个绑定.
归档时间: |
|
查看次数: |
5095 次 |
最近记录: |