【问题标题】:ContextMenu CommandParamater binding?ContextMenu CommandParamater 绑定?
【发布时间】:2011-07-19 23:50:09
【问题描述】:

我正在尝试将我的ContextMenu 项目的commandParamater 绑定到表单上的另一个元素,但是无论我尝试什么,commandParamater 始终是null

谁能告诉我如何正确绑定上下文菜单项的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>

【问题讨论】:

标签: .net wpf xaml binding contextmenu


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    相关资源
    最近更新 更多