2011-07-19 69 views
1

我試圖將ContextMenu項的commandParamater綁定到窗體上的另一個元素,但不管我嘗試的是什麼commandParamater始終是nullContextMenu 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

可能重複在文本菜單從菜單項綁定](http://stackoverflow.com/questions/1013558/elementname-binding-from-menuitem-in-contextmenu) – CodeNaked

回答

1

這是WPF遇到了一個非常普遍的問題;上下文菜單本身並不是與定義的控件相同的可視化樹的一部分,因此無法使用ElementNameRelativeSource綁定。

我最近也遇到過這個問題,使用Tag和PlacementTarget的解決方案對我來說工作得很好。

這裏是具有不同的解決方案,該問題的部分訊息(除了一個CodeNaked建議) -

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

的[的ElementName
相關問題