0

我有两个用户控件LeftPanelDeviceList. DeviceList在里面LeftPanel。我希望设备列表中的上下文菜单能够调用父视图模型上的命令,该模型设置在承载DeviceList. 我尝试了以下方法,但它不起作用。

这是里面的 ContextMenuDeviceList

<MenuItem Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                    AncestorType={x:Type local:LeftPanel}},
                    Path=DeviceListViewModel.RemoveDevice}">

这是LeftPanel用户控件

<UserControl x:Class="Tool.LeftPanel" .... >
    <Grid DataContext="{Binding DeviceListViewModel}" Grid.Column="1">
        <local:DeviceList Grid.Row="1" Margin="0,0,0,10"/>
4

2 回答 2

0

我遇到了类似的问题,但是 ContextMenu 看不到 ViewModel 或 Parent 的 DataContext。一个可能的解决方案是Access ViewModel / DataConext inside ContextMenu

于 2013-05-31T10:44:18.827 回答
0

上下文菜单不是可视化树的一部分,所以我认为您的绑定完全是错误的,因为数据上下文不是您所想的。

使用Snoop在运行时检查您的 DataContext 和绑定

我假设您必须在绑定中使用 PlacementTarget

于 2013-05-31T10:45:05.980 回答