2013-05-31 129 views
0

我有兩個用戶控件LeftPanelDeviceListDeviceList位於LeftPanel之內。我希望設備列表中的上下文菜單能夠調用父視圖模型上的命令,該模型設置在承載DeviceList的網格上。我嘗試了以下,但它不起作用。將ICommand綁定到父ViewModel

這裏是文本菜單中DeviceList

<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"/> 
+0

我說不做這與PlacementTarget工作。讓你的'DeviceListView'在'DevicelistViewModel'中調用一個命令,並從該VM發送一條「消息」到它的父虛擬機並相應地執行。不要在View的跨越多個虛擬機的路線上走下去。很快就會讓人感到困惑 – Viv

回答

0

contextmenu不是visualtree的一部分,所以我認爲你的綁定是完全錯誤的,因爲datacontext不是你所想的。

使用Snoop檢查您的DataContext和綁定在運行時

我假設你有你的綁定

+0

嗯,這是有道理的 – Chris

相關問題