我有一个treeView项目的模板:
<HierarchicalDataTemplate x:Key="RatesTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=ID}"/>
<Button CommandParameter="{Binding Path=ID}"
Command="{Binding ElementName=CalcEditView, Path=DataContext.Add}">Add</Button>
</StackPanel>
</HierarchicalDataTemplate>
作为一个DataContext,我有一个带有非空ID字段的linq实体。
问题是:如果我使用具有CanExecutedMethod的DelegateCommand 'Add':
AddRate = new DelegateCommand<int?>(AddExecute,AddCanExecute);
这个方法只被调用一次且参数为null(而textBlock显示正确的ID值)。在调试器中可以看到,CanExecute是在调用ID属性之前就被调用了。看起来在绑定到实际参数之前,wpf会调用CanExecute并忘记它。一旦绑定完成和正确的值被加载后,它不再调用CanExecute。
作为解决方法,我可以使用仅带有execute委托的命令:
Add = new DelegateCommand<int?>(AddExecute);
AddExecute被正确的ID值调用,并且运行得很完美。但我仍然想使用CanExecute功能。有什么建议吗?