【发布时间】:2010-06-12 02:17:11
【问题描述】:
我有一个棘手的问题,我将ContextMenu 绑定到一组ICommand 派生对象,并通过样式在每个MenuItem 上设置Command 和CommandParameter 属性:
<ContextMenu
ItemsSource="{Binding Source={x:Static OrangeNote:Note.MultiCommands}}">
<ContextMenu.Resources>
<Style
TargetType="MenuItem">
<Setter
Property="Header"
Value="{Binding Path=Title}" />
<Setter
Property="Command"
Value="{Binding}" />
<Setter
Property="CommandParameter"
Value="{Binding Source={x:Static OrangeNote:App.Screen}, Path=SelectedNotes}" />
...
然而,虽然ICommand.Execute( object ) 得到了应有的选定注释集,但ICommand.CanExecute( object )(在创建菜单时调用)被传递为空。我已经检查并且在调用之前选择的笔记集合被正确地实例化(实际上它在它的声明中被分配了一个值,所以它永远不是null)。我不明白为什么 CanEvaluate 会通过null。
【问题讨论】:
-
我遇到了完全相同的问题。我的解决方案是在命令参数之后绑定命令,只需将命令参数的设置器放在命令设置器之前,然后绑定的参数突然被传递给
CanExecute的第一次调用。
标签: c# wpf data-binding contextmenu icommand