CommandGroup CanExecute不能偶尔使用

时间:2013-05-11 03:49:20

标签: c# wpf .net-4.0 command

我一直试图追踪这一段时间已经有一段时间了,并且总是空白,所以也许有些东西我想念别人可能会看到的?注意:目前这只能在一台QA机器上看到,因此我不能像普通的

那样进行调试

我正在使用Josh Smith's code project中的命令组代码,并在此帖子的底部实现了CommandReference

问题是绑定到CommandGroup的按钮被禁用,但其他两个则没有。请注意,CommandGroup按钮只是其他两个按钮的串联。因此,如果两者都启用了,那么CommandGroup按钮也是如此。所以,我猜这与CommandGroupCommandReference有关......任何想法都会有所帮助。

我目前的工作假设是ApplicationCommands.CloseICommand视为CommandGroup与其正常的RoutedUICommand相对应是问题所在。特别是因为我可以通过直接调用ApplicationCommands.Close.CanExecute(null)来创建绑定到同一命令的两个按钮之间的不平衡。但是,我不知道如何解决这个问题......

如果RoutedCommand在没有CanExecute的情况下被调用,则

FilterInputElement(Keyboard.FocusedElement)的{​​{1}} CanExecute使用此IInputElement ....但在我正在尝试的情况下,无论它来自哪里,我都称ApplicationCommands.Close为同一个

CommandGroup

<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
}

1 个答案:

答案 0 :(得分:0)

我仍然在等待质量保证来验证这一点,但我能够得到一个类似的问题,我可以重现,结果发现我的ICommand需要设置为

public event EventHandler CanExecuteChanged
{
  add { CommandManager.RequerySuggested += value; }
  remove { CommandManager.RequerySuggested -= value; }
}

而不是

public event EventHandler CanExecuteChanged;

public void RaiseCanExecuteChanged()...