2011-07-19 7 views
1

私のContextMenuアイテムのcommandParamaterをフォーム上の別のエレメントにバインドしようとしていますが、何を試してもは常にnullです。ContextMenu CommandParamaterバインディング?

私のコンテキストメニューのcommandParamaterを正しくバインドする方法を教えてもらえますか?

私は何を持っている:

<TreeView.ItemTemplate> 
    <HierarchicalDataTemplate ItemsSource="{Binding Path=Files}"> 
     <Grid> 
      <Grid.ContextMenu> 
       <ContextMenu> 
       <MenuItem Header="Rename Folder" 
          Command="{Binding Path=ToggleControlVisability}" 
          CommandTarget="{Binding ElementName=FolderEditor}" 
          CommandParameter="{Binding ElementName=FolderEditor}"> 
       </MenuItem> 
       </ContextMenu> 
      </Grid.ContextMenu> 

      <Label Content="{Binding Path=FolderName}"></Label> 

      <StackPanel Name="FolderEditor" Orientation="Horizontal" 
         Visibility="Hidden"> 
       <TextBox Text="{Binding Path=FolderName}"></TextBox> 
      </StackPanel> 
     </Grid> 
    </HierarchicalDataTemplate> 
</TreeView.ItemTemplate> 
+0

- ここ

は、この問題に対する別の解決策を有するいくつかの記事(離れCodeNakedが提案ものから)であります](http://stackoverflow.com/questions/1013558/elementname-binding-from-menuitem-in-contextmenu) – CodeNaked

答えて

1

これは、WPFで遭遇非常に共通の問題です。コンテキストメニュー自体は、定義されたコントロールと同じビジュアルツリーの一部ではありません。このため、ElementNameまたはRelativeSourceバインディングを使用することはできません。

私も最近この問題に直面しており、タグとPlacementTargetを使用した解決策はうまく機能しました。たContextMenu内のMenuItemから結合【のElementNameの可能重複

How to set CommandTarget for MenuItem inside a ContextMenu?

http://www.sevensteps.com/binding-contextmenu-commands-in-wpf-to-the-controls-viewmodel.ashx

http://www.ikriv.com/blog/?p=434