将ICommand绑定到父ViewModel

时间:2013-05-31 10:28:21

标签: c# .net wpf xaml

我有两个用户控件LeftPanelDeviceListDeviceList位于LeftPanel内。我希望设备列表中的上下文菜单能够在父视图模型上调用命令,该模型在托管DeviceList的网格上设置。我尝试过以下但是没有用。

以下是DeviceList

中的ContextMenu
<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"/>

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

contextmenu不是visualtree的一部分,所以我认为你的绑定完全错误,因为datacontext不是你想到的。

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

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

相关问题