2013-05-11 18 views
0

我一直在試圖追查這一段時間,現在正在繪製一個完整的空白,所以也許有一些我錯過了別人可能會看到的東西?注意:目前只有在一臺QA機器上才能看到,因此我無法像正常那樣進行調試。CommandGroup CanExecute不能只是零星地工作

我使用的是Josh Smith's code project的命令組代碼,CommandReference在本文底部實現。

問題是綁定到CommandGroup的按鈕被禁用,但其他兩個不是。請注意,CommandGroup按鈕只是其他兩個的並置。所以,它表明,如果兩者都啓用,那麼按鈕應該是CommandGroup。所以,我猜這與CommandGroupCommandReference有關......任何想法都會有幫助。

我目前的工作假設是ApplicationCommands.Close被視爲ICommandCommandGroup與其正常的RoutedUICommand相比是問題。特別是因爲我可以通過直接調用ApplicationCommands.Close.CanExecute(null)來創建綁定到相同命令的兩個按鈕之間的不平衡。但是,我不知道如何解決這個...

RoutedCommandCanExecute使用此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}"/> 

命令參考:

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

在您的命令參考中,不是'SaveDeviceCommand',而是'SaveCommand'。這個錯字只是出現在這裏嗎?還是它是你的代碼的一部分? – XAMeLi 2013-05-11 06:53:59

+0

不,這是正確的,注意它使用CommandReference鍵 – 2013-05-11 13:36:18

+0

哦,錯過了。不能確切地把我的手指放在原因上,但似乎'CommandReference'嵌套在它看起來比一見鍾情。單獨使用「ICommand」,所有屬性被稱爲「Command」,都會讓人非常困惑,以至於無法正確執行。試試這個,在你的CommandGroup的CommandReference中,將Command屬性的綁定改爲:'Command =「{Binding Source = {StaticResource SaveCommand},Path = Command}」' – XAMeLi 2013-05-12 08:12:29

回答

0

我仍然對QA等待驗證這一點,但是我能得到一個類似的問題,我可以重現,並且事實證明,我的ICommand需要被設置爲

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

,而不是

public event EventHandler CanExecuteChanged; 

public void RaiseCanExecuteChanged()...