我有兩個用戶控件LeftPanel
和DeviceList
。 DeviceList
位於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"/>
我說不做這與PlacementTarget工作。讓你的'DeviceListView'在'DevicelistViewModel'中調用一個命令,並從該VM發送一條「消息」到它的父虛擬機並相應地執行。不要在View的跨越多個虛擬機的路線上走下去。很快就會讓人感到困惑 – Viv