繁体   English   中英

将contextMenu绑定到与treeview不同的viewmodel

[英]Bind contextMenu to a different viewmodel from treeview

当我在Treeview项目上使用鼠标右键时,我想显示一个contextMenu项目。

之后,我想在单击MenuItem时使用命令,但是我需要将该命令与其他视图模型绑定,并将命令参数与从我的Treeview所选项目中获得的良好ViewModel绑定。

所以目前,我有这样的事情:

<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}

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

我也尝试使用它,但是它也没有用:

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

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

UserControl不是MenuItem的可视祖先,因为ContextMenu驻留在其自己的可视树中。

TextBlockTag属性绑定到UserControl ,然后将Command属性绑定到ContextMenuPlacementTarget

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM