TreeView控件的ContextMenu MVVM绑定 [英] TreeView ContextMenu MVVM Binding
问题描述
我现在有一个使用MVVM模型用户控件。
在该控件有一个TreeView,它显示一些项目。我添加了一个HierarchicalDataTemplate为此TreeView和在该模板的项目将ContextMenu。
在视图模型,这是DataContext的控制(命名RestoresTreeViewControl)是我要绑定的菜单项目之一的命令。但是我做了什么似乎并不奏效。我越来越平常绑定参考无法找到来源。
下面是$ C $的c为试图将EditDatabaseCommand绑定到菜单项中的一个的DataTemplate中的比特。
< HierarchicalDataTemplate数据类型={X:类型模式:数据库}>
<&StackPanel的GT;
< TextBlock的文本={绑定名称}>
< TextBlock.ContextMenu>
<&文本菜单GT;
<菜单项标题=编辑命令={绑定的ElementName = RestoresTreeViewControl,路径= DataContext.EditDatabaseCommand}/>
<菜单项标题=删除/>
<分离器/>
<菜单项标题=测试连接/>
< /文本菜单>
< /TextBlock.ContextMenu>
< / TextBlock的>
< / StackPanel的>
< / HierarchicalDataTemplate>
下面是视图模型的一个部分,在那里的命令。
公共ICommand的EditDatabaseCommand {搞定;私人集; }
不幸的是,文本菜单不在的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.
这篇关于TreeView控件的ContextMenu MVVM绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!