且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

TreeView控件的ContextMenu MVVM绑定

更新时间:2022-10-15 08:05:10

不幸的是,文本菜单不在的VisualTree,所以它不是会看到你的DateContext。你可以做的就是这样的事情(从这里复制: MVVM结合命令文本菜单项

 <按钮高度=40保证金=0,2,0,0CommandParameter ={绑定名称}
标签={绑定的RelativeSource = {的RelativeSource模式= FindAncestor,
      AncestorType = {X:类型用户控件}}}命令={结合
      的RelativeSource = {的RelativeSource模式= FindAncestor,AncestorType = {X:类型用户控件}},
      路径= DataContext.ConnectCommand}>
        < Button.ContextMenu>
            <&文本菜单GT;
                <菜单项标题=删除
               CommandParameter ={绑定名称}
            命令={绑定路径= PlacementTarget.Tag.DataContext.RemoveCommand,
            的RelativeSource = {的RelativeSource模式= FindAncestor,AncestorType =文本菜单}}/>
            < /文本菜单>
< /Button.ContextMenu>

所以,简单地用PlacementTarget.Tag找到你的视图模型。

I currently have a UserControl that uses the MVVM model.

In that control there is a TreeView, which displays some items. I have added a HierarchicalDataTemplate for this TreeView and in that template is a ContextMenu for the Items.

In the ViewModel, which is DataContext of the control (named RestoresTreeViewControl) is a command I want to bind one of the menu items to. However what I have done doesn't seem to be working. I am getting the usual can't find source for binding reference.

Here is the bit of code for the datatemplate that tried to bind the EditDatabaseCommand to one of the menu items.

<HierarchicalDataTemplate DataType="{x:Type model:Database}" >
                    <StackPanel>
                        <TextBlock Text="{Binding Name}" >
                            <TextBlock.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="Edit" Command="{Binding ElementName=RestoresTreeViewControl, Path=DataContext.EditDatabaseCommand}" />
                                    <MenuItem Header="Delete"/>
                                    <Separator/>
                                    <MenuItem Header="Test Connection"/>
                                </ContextMenu>
                            </TextBlock.ContextMenu>
                        </TextBlock>
                    </StackPanel>
                </HierarchicalDataTemplate>

Here is a section of the ViewModel where the command is.

public ICommand EditDatabaseCommand { get; private set; }

Unfortunately the ContextMenu is not in the VisualTree, so it's not gonna see your DateContext. What you can do is something like this (copied from here: MVVM binding command to contextmenu item)

<Button Height="40" Margin="0,2,0,0" CommandParameter="{Binding Name}" 
Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
      AncestorType={x:Type UserControl}}}" Command = "{Binding 
      RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}},
      Path=DataContext.ConnectCommand}">
        <Button.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Remove" 
               CommandParameter="{Binding Name}"
            Command="{Binding Path=PlacementTarget.Tag.DataContext.RemoveCommand,
            RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/>
            </ContextMenu>
</Button.ContextMenu>

So simply use PlacementTarget.Tag to find your ViewModel.