WPF CommandParameter绑定和canExecute

我有一个treeView项的模板:

      

作为DataContext,我有一个ID为非空字段的linq实体。

问题是:如果我使用CanExecutedMethod使用DelegateCommand’Add’:

 AddRate = new DelegateCommand(AddExecute,AddCanExecute); 

它只调用一次,参数为null(而textBlock显示正确的ID值)。 在调用ID属性之前调用CanExecute(使用调试器检查)。 似乎在绑定到实际参数之前,wpf正在调用canExecute并忘记它。 绑定完成并加载适当的值后,它不会再次调用CanExecute。

作为一种解决方法,我可以使用只有执行委托的命令:

 Add = new DelegateCommand(AddExecute); 

使用正确的ID值调用AddExecute并且工作正常。 但我仍然想使用CanExecutefunction。 有任何想法吗?

在这种情况下,最好在用作Command参数的属性上调用RaiseCanExecuteChanged()。 在您的情况下,它将是您的ViewModel中的ID属性(或者您正在使用的任何DataContext)。

它会是这样的:

 private int? _id; public int? ID { get { return _id; } set { _id = value; DelegateCommand command = ((SomeClass)CalcEditView.DataContext).Add; command.RaiseCanExecuteChanged(); } } 

效果与您的解决方案相同,但它使Command逻辑不受代码隐藏的影响。

您可以尝试使用CommandManager.InvalidateRequerySuggested强制更新。

我使用参数作为object ,然后将其强制转换为int

 Add = new DelegateCommand(add, canAdd); 

并添加方法

 void add(object parameter){ int id = Convert.ToInt32(parameter); //or simply int id2 = (int)parameter; //... // do your stuff //... }