0

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

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

这是我的 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");
    }

提前致谢。

4

1 回答 1

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 >
于 2013-09-18T18:08:17.513 回答