2010-09-12 31 views
0

我指定一個TreeViewItem的控件模板中的上下文菜單如下:WPF菜單項的樣式參數不提供菜單上首先打開

        <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> 

其中命令是ICommandViewModels列表具有以下簽名對象:

public interface ICommandViewModel 
    { 
     string Name { get; } 
     Image Icon { get; } 
     ICommand Command { get; set; } 
     object CommandParameter { get; set; } 

    } 

當ContextMenu打開時,傳遞給Command的CommandParamter最初爲空,它將按照指定禁用Command。如果Command.CanExecute總是返回true,這不是問題,因爲Command.Execute最終會得到正確的CommandParameter。在某些情況下,如果CommandParamter爲null,則不允許執行Command,因此這會成爲問題。

任何人都有關於這裏發生了什麼的理論和可能的修復?

TIA。

回答

2

嘗試調換CommandParameterCommand屬性設置器。我不確定這種行爲的原因,但看起來好像它不能在CommandParameter上執行更改。另一種方法是修復這裏描述http://compositewpf.codeplex.com/Thread/View.aspx?ThreadId=47338

+0

由「swythan」提供的codeplex修復程序是我最終使用,它工作得很好。 – 2013-12-19 21:53:36