我在TreeViewItem的ControlTemplate中指定了一个上下文菜单,如下所示:
<ContextMenu ItemsSource="{Binding Commands}">
<ContextMenu.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Command" Value="{Binding Command}" />
<Setter Property="CommandParameter" Value="{Binding CommandParameter}" />
<Setter Property="Header" Value="{Binding Name}" />
<Setter Property="Icon" Value="{Binding Icon}" />
</Style>
</ContextMenu.ItemContainerStyle>
</ContextMenu>
其中Commands是具有以下签名的ICommandViewModels对象的列表:
public interface ICommandViewModel
{
string Name { get; }
Image Icon { get; }
ICommand Command { get; set; }
object CommandParameter { get; set; }
}
当ContextMenu打开时,传递给Command的CommandParamter最初为null,这将禁用指定的Command。如果Command.CanExecute总是返回true,这不是问题,因为Command.Execute最终会获得正确的CommandParameter。在某些情况下,如果CommandParamter为null,则不允许执行Command,因此这就成了一个问题。
任何人都有关于这里发生了什么的理论和可能的解决办法?
TIA。
答案 0 :(得分:2)
尝试交换CommandParameter
和Command
属性设置器。我不确定这种行为的原因,但看起来它不会重新查询可以在CommandParameter上执行更改。另一种解决方法是http://compositewpf.codeplex.com/Thread/View.aspx?ThreadId=47338