[英]Bind ICommand to parent ViewModel
I have two user controls LeftPanel
and DeviceList
. 我有两个用户控件LeftPanel
和DeviceList
。 DeviceList
is inside the LeftPanel
. DeviceList
在LeftPanel
内部。 I want the context menu in device list to be able to call a command on the parent view model, which is set on a grid that hosts DeviceList
. 我希望设备列表中的上下文菜单能够在父视图模型上调用命令,该视图在承载DeviceList
的网格上设置。 I have tried the following but it does not work. 我已经尝试了以下方法,但是它不起作用。
Here is the ContextMenu within DeviceList
这是DeviceList
的ContextMenu
<MenuItem Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type local:LeftPanel}},
Path=DeviceListViewModel.RemoveDevice}">
Here is the LeftPanel
usercontrol 这是LeftPanel
用户控件
<UserControl x:Class="Tool.LeftPanel" .... >
<Grid DataContext="{Binding DeviceListViewModel}" Grid.Column="1">
<local:DeviceList Grid.Row="1" Margin="0,0,0,10"/>
I had a similar problem, but the ContextMenu does not see the DataContext of the ViewModel or the Parent. 我有一个类似的问题,但是ContextMenu看不到ViewModel或Parent的DataContext。 A possible solution is here Access ViewModel / DataConext inside ContextMenu . 一个可能的解决方案是在ContextMenu中访问ViewModel / DataConext 。
the contextmenu is not part of the visualtree, so i think your binding is simply wrong because the datacontext is not what you think of. 上下文菜单不是视觉树的一部分,所以我认为您的绑定完全是错误的,因为数据上下文不是您所想的。
use Snoop to check your DataContext and bindings at runtime 使用Snoop在运行时检查您的DataContext和绑定
i assume that you have to work with PlacementTarget in your bindings 我认为您必须在绑定中使用PlacementTarget
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.