[英]CommandGroup CanExecute not working only sporadically
I have been trying to track this down for quite a while now and am drawing a total blank, so maybe there is something I am missing that others might see? 我已经尝试追踪了很长时间,并且正在绘制一个空白,所以也许我缺少其他人可能看到的东西? NOTE: this is currently only being seen on one QA machine, thus why I cannot debug this like normal
注意:目前仅在一台QA机器上看到此错误,因此为什么我无法像平常一样调试它
I am using the Command Group code from Josh Smith's code project with CommandReference
implemented at the bottom of this post. 我正在使用Josh Smith的代码项目中的Command Group代码,并在本文的底部实现了
CommandReference
。
The problem is that the button that is bound to the CommandGroup
is disabled, however the other two are not. 问题是绑定到
CommandGroup
的按钮被禁用,而其他两个按钮没有被禁用。 Notice that the CommandGroup
button is just a concatenation of the other two. 请注意,
CommandGroup
按钮只是其他两个的串联。 So, it stands that if both are enabled, then so should the CommandGroup
button. 因此,如果两个都启用,那么
CommandGroup
按钮也应该启用。 So, I am guessing this has to do with either the CommandGroup
or CommandReference
...any ideas would be helpful. 因此,我猜想这与
CommandGroup
或CommandReference
...任何想法都会有所帮助。
My current working hypothesis is that ApplicationCommands.Close
being treated as an ICommand
in the CommandGroup
versus its normal RoutedUICommand is the problem. 我目前的工作假设是将
ApplicationCommands.Close
视为CommandGroup
的ICommand
而不是其常规RoutedUICommand。 Especially as I can create an imbalance between two buttons bound to the same command by calling ApplicationCommands.Close.CanExecute(null)
directly. 特别是因为我可以通过直接调用
ApplicationCommands.Close.CanExecute(null)
在绑定到同一命令的两个按钮之间创建不平衡。 But, I am not sure how to resolve this... 但是,我不确定该如何解决...
RoutedCommand
's CanExecute
uses this FilterInputElement(Keyboard.FocusedElement)
if CanExecute
is called without an IInputElement
....but in the case I am trying just above, I am calling ApplicationCommands.Close
the same no matter where it's origin is from RoutedCommand
的CanExecute
使用此FilterInputElement(Keyboard.FocusedElement)
如果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}"/>
Command Reference: 命令参考:
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
}
I am still waiting on QA to verify this, however I was able to get a similar issue that I could reproduce, and it turned out that my ICommand
needed to be set up as 我仍在等待质量检查来验证这一点,但是我能够得到一个类似的问题,可以重现,结果发现我的
ICommand
需要设置为
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
instead of 代替
public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged()...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.