将Context菜单绑定到不在上下文的Visuals树中的ViewModel

时间:2013-09-18 16:35:30

标签: wpf xaml binding treeview contextmenu

我有一个TreeView,其itemsource是我的Model类的集合。我在treeView上添加了一个上下文菜单。由于contextMenu的命令应该在可视树中,所以我不得不将它们放在我的Model类中。哪个错误(将模型绑定到目录)。

如何将我的上下文菜单命令绑定到我的ViewModel而不是Model?

这是我的TreeView的XAML代码。

 <TreeView ItemsSource="{Binding DigSource}" Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}">
                    <TreeView.ItemContainerStyle >
                        <Style TargetType="{x:Type TreeViewItem}">
                            <Setter Property="ContextMenu">
                                <Setter.Value>
                                    <ContextMenu >
                                        <MenuItem Header="Edit" CommandParameter="{Binding }"
                                            Command="{Binding Path=PlacementTarget.Tag.DataContext.MyCommand, 
                                            RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}" />
                                    </ContextMenu>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </TreeView.ItemContainerStyle >
                    <TreeView.ItemTemplate>
                        <HierarchicalDataTemplate ItemsSource="{Binding SingleDig}">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Name}"/>
                            </StackPanel>
                            <HierarchicalDataTemplate.ItemTemplate>
                                <DataTemplate DataType="{x:Type dt:MultiDig}">
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding Name}"/>
                                    </StackPanel>
                                </DataTemplate>
                            </HierarchicalDataTemplate.ItemTemplate>
                        </HierarchicalDataTemplate>
                    </TreeView.ItemTemplate>
                </TreeView>

和ViewModel的Icommand属性是

    private ICommand _editMenuCommand;
    /// <summary>
    /// Edit command
    /// </summary>
    public ICommand EditMenuCommand { get { return _editMenuCommand ?? (_editMenuCommand = new RelayCommand(EditMenu)); } }

    /// <summary>
    /// Edit menu
    /// </summary>
    public void EditMenu()
    {
        MessageBox.Show("Edit me");
    }

提前致谢。

1 个答案:

答案 0 :(得分:3)

这是你的ItemContainerStyle,带有更新的绑定。在我的结尾测试过,效果很好。

<TreeView ItemsSource="{Binding DigSource}">
    <TreeView.ItemContainerStyle >
          <Style TargetType="{x:Type TreeViewItem}">
               <Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"/>
               <Setter Property="ContextMenu">
                   <Setter.Value>
                       <ContextMenu >
                            <MenuItem Header="Edit" CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                                                Command="{Binding Path=PlacementTarget.Tag.DataContext.MyCommand, 
                                                RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}" />
                         </ContextMenu>
                    </Setter.Value>
                  </Setter>
         </Style>
   </TreeView.ItemContainerStyle >