我有两个用户控件LeftPanel
和DeviceList
。 DeviceList
位于LeftPanel
内。我希望设备列表中的上下文菜单能够在父视图模型上调用命令,该模型在托管DeviceList
的网格上设置。我尝试过以下但是没有用。
以下是DeviceList
<MenuItem Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type local:LeftPanel}},
Path=DeviceListViewModel.RemoveDevice}">
这是LeftPanel
usercontrol
<UserControl x:Class="Tool.LeftPanel" .... >
<Grid DataContext="{Binding DeviceListViewModel}" Grid.Column="1">
<local:DeviceList Grid.Row="1" Margin="0,0,0,10"/>
答案 0 :(得分:0)
我遇到了类似的问题,但ContextMenu没有看到ViewModel或Parent的DataContext。 可能的解决方案是Access ViewModel / DataConext inside ContextMenu。
答案 1 :(得分:0)
contextmenu不是visualtree的一部分,所以我认为你的绑定完全错误,因为datacontext不是你想到的。
使用Snoop在运行时检查DataContext和绑定
我假设您必须在绑定中使用PlacementTarget