【发布时间】:2013-05-05 19:18:02
【问题描述】:
我一直在努力追查这个问题已经有一段时间了,我画的是一片空白,所以也许我遗漏了一些其他人可能会看到的东西?注意:这目前只能在一台 QA 机器上看到,因此我无法正常调试它
我正在使用来自Josh Smith's code project 的命令组代码和在本文底部实现的CommandReference。
问题是绑定到CommandGroup 的按钮被禁用,但其他两个没有。请注意,CommandGroup 按钮只是其他两个按钮的串联。因此,如果两者都启用,那么CommandGroup 按钮也应该启用。所以,我猜这与CommandGroup 或CommandReference 有关......任何想法都会有所帮助。
我目前的工作假设是 ApplicationCommands.Close 在 CommandGroup 中被视为 ICommand 而不是其正常的 RoutedUICommand 是问题所在。特别是因为我可以通过直接调用ApplicationCommands.Close.CanExecute(null) 在绑定到同一命令的两个按钮之间创建不平衡。但是,我不确定如何解决这个问题......
RoutedCommand 的CanExecute 使用这个FilterInputElement(Keyboard.FocusedElement) 如果CanExecute 在没有IInputElement 的情况下被调用....但是在我上面尝试的情况下,我调用ApplicationCommands.Close 同样没有不管它来自哪里
指挥组
<Button Content="OK" IsDefault="True">
<Button.Resources>
<commonCommands:CommandReference x:Key="SaveCommand" Command="{Binding SaveDeviceCommand}"/>
</Button.Resources>
<Button.Command>
<commonCommands:CommandGroup>
<commonCommands:CommandGroup.Commands>
<commonCommands:CommandReference Command="{StaticResource SaveCommand}"/>
<x:Static Member="ApplicationCommands.Close"/>
</commonCommands:CommandGroup.Commands>
</commonCommands:CommandGroup>
</Button.Command>
</Button>
<Button Content="Cancel" Command="ApplicationCommands.Close" IsCancel="True"/>
<Button Content="Apply" Command="{Binding SaveDeviceCommand}"/>
命令参考:
public class CommandReference : Freezable, ICommand
{
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof (ICommand), typeof (CommandReference), new PropertyMetadata(OnCommandChanged));
public ICommand Command
{
get { return (ICommand) GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
#region ICommand Members
public bool CanExecute(object parameter)
{
return Command != null && Command.CanExecute(parameter);
}
public void Execute(object parameter)
{
Command.Execute(parameter);
}
public event EventHandler CanExecuteChanged;
private static void OnCommandChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
{
var commandReference = dependencyObject as CommandReference;
if (commandReference == null) return;
var oldCommand = eventArgs.OldValue as ICommand;
var newCommand = eventArgs.NewValue as ICommand;
if (oldCommand != null)
oldCommand.CanExecuteChanged -= commandReference.CanExecuteChanged;
if (newCommand != null)
newCommand.CanExecuteChanged += commandReference.CanExecuteChanged;
}
#endregion
#region Freezable
protected override Freezable CreateInstanceCore()
{
throw new NotImplementedException();
}
#endregion
}
【问题讨论】:
-
在你的命令参考中,你有
SaveCommand而不是SaveDeviceCommand。这个错字是只出现在这里还是你的代码的一部分? -
不,这是正确的,注意它正在使用 CommandReference 键
-
哦,错过了。不能完全说明原因,但似乎
CommandReference中的嵌套比乍一看要多。单独作为ICommand,并且所有属性都称为“命令”,正确遵循非常令人困惑。试试这个,在您的 CommandGroup 中的 CommandReference 上,将 Command 属性的绑定更改为:Command="{Binding Source={StaticResource SaveCommand}, Path=Command}" -
@XAMeLi 你是对的,我不知道我是如何错过嵌套的。明天我会修复它,如果能解决问题,请告诉您
-
@XAMeLi 好吧,QA人员终于测试了这个,还是不行....