将 contextMenu 绑定到与树视图不同的视图模型 [英] Bind contextMenu to a different viewmodel from treeview

查看:26
本文介绍了将 contextMenu 绑定到与树视图不同的视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在树视图项上使用右键单击时显示 contextMenu 项.

I want to show a contextMenu item when I am using rightclick on a treeview item.

之后,我想在单击 MenuItem 时使用命令,但我需要将命令与不同的视图模型绑定,并将命令参数与来自我的树视图所选项目的良好视图模型绑定.

After that, I want to use a command when I click on my MenuItem, but I need to bind the command with a different viewmodel and the command parameter with the good viewmodel who come from my treeview selected item.

所以目前,我有类似的东西:

So for the moment, I have something like that :

<TreeView x:Name="TreeViewProtocolsAndEquipments" AllowDrop="True"
        ItemsSource="{Binding ModuleParams}">

        <TreeView.Resources>
            <!-- CONTEXT MENU -->
            <!-- Protocol -->    
            <ContextMenu x:Key="ContextMenuProtocol">
                <MenuItem Header="Add new equipment" Command="{Binding AddNewEquipmentCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}">
                    <MenuItem.Icon>
                        <Image Source="Images/Add.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <Separator />
            </ContextMenu>

            <!-- MODULE XXX -->
            <!-- ModuleParam > xxx -->
            <HierarchicalDataTemplate DataType="{x:Type xxx:ModuleParamXXXViewModel}" ItemsSource="{Binding ModuleItems}">
                <TextBlock Text="XXX" Foreground="Green" ContextMenu="{StaticResource ContextMenuProtocol}"/>
            </HierarchicalDataTemplate>
        </TreeView.Resources>

    </TreeView>

目前我的命令是绑定到 xxx:ModuleParamXXXViewModel 如果我只是让 { binding }

For the moment my command is bind to xxx:ModuleParamXXXViewModel if I just let { binding }

  1. 我能否将我的 Command 绑定到我的 ActivatedProtocolsAndEquipmentsTreeViewModel(此用户控件的数据上下文)并保留我的 CommandParameter 我的 xxx:ModuleParamXXXViewModel(我们触发右键单击以显示 contextMenu 的树视图中的项目)?
  2. 如何通过 MVVM 实践以其他方式实现这一目标?

我也尝试过使用它,但它也不起作用:

I also tried to use this but it didn't work too :

<MenuItem Header="Add new equipment" Command="{Binding Path=DataContext.AddNewEquipmentCommand, Source={x:Reference TreeViewProtocolsAndEquipments}}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}">

有了这个,我得到未将对象引用设置为对象的实例

推荐答案

UserControl 不是 MenuItem 的视觉祖先,因为 ContextMenu> 驻留在自己的可视化树中.

The UserControl is not a visual ancestor of the MenuItem since a ContextMenu resides in its own visual tree.

TextBlockTag属性绑定到UserControl,然后将Command属性绑定到<ContextMenu 的 code>PlacementTarget:

Bind the Tag property of the TextBlock to the UserControl and then bind the Command property to the PlacementTarget of the ContextMenu:

<TreeView x:Name="TreeViewProtocolsAndEquipments" AllowDrop="True"
                  ItemsSource="{Binding ModuleParams}">
    <TreeView.Resources>
        <!-- CONTEXT MENU -->
        <!-- Protocol -->
        <ContextMenu x:Key="ContextMenuProtocol">
            <MenuItem Header="Add new equipment"
                              Command="{Binding PlacementTarget.Tag.DataContext.AddNewEquipmentCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}" 
                              CommandParameter="{Binding}">
                <MenuItem.Icon>
                    <Image Source="Images/Add.png" />
                </MenuItem.Icon>
            </MenuItem>
            <Separator />
        </ContextMenu>

        <!-- MODULE XXX -->
        <!-- ModuleParam > xxx -->
        <HierarchicalDataTemplate DataType="{x:Type xxx:ModuleParamXXXViewModel}" ItemsSource="{Binding ModuleItems}">
            <TextBlock Text="XXX" Foreground="Green"
                               Tag="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}"
                               ContextMenu="{StaticResource ContextMenuProtocol}"/>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

这篇关于将 contextMenu 绑定到与树视图不同的视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆