2017-08-23 123 views
1

我想在顯示一個contextMenu項目時使用右鍵單擊樹視圖項目。將contextMenu綁定到樹視圖的不同視圖模型

之後,我想使用一個命令,當我點擊我的MenuItem,但我需要綁定命令與不同的viewmodel和命令參數與良好的viewmodel誰來自我的treeview選定的項目。

所以,就目前而言,我有這樣的事情:

<TreeView x:Name="TreeViewProtocolsAndEquipments" AllowDrop="True" 
     ItemsSource="{Binding ModuleParams}"> 

     <TreeView.Resources> 
      <!-- CONTEXT MENU --> 
      <!-- Protocol -->  
      <ContextMenu x:Key="ContextMenuProtocol"> 
       <MenuItem Header="Add new equipment" Command="{Binding AddNewEquipmentCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}"> 
        <MenuItem.Icon> 
         <Image Source="Images/Add.png" /> 
        </MenuItem.Icon> 
       </MenuItem> 
       <Separator /> 
      </ContextMenu> 

      <!-- MODULE XXX --> 
      <!-- ModuleParam > xxx --> 
      <HierarchicalDataTemplate DataType="{x:Type xxx:ModuleParamXXXViewModel}" ItemsSource="{Binding ModuleItems}"> 
       <TextBlock Text="XXX" Foreground="Green" ContextMenu="{StaticResource ContextMenuProtocol}"/> 
      </HierarchicalDataTemplate> 
     </TreeView.Resources> 

    </TreeView> 

對於我的命令被綁定到時刻XXX:ModuleParamXXXViewModel如果我只是讓{結合}

  1. 我可以綁定我命令到我ActivatedProtocolsAndEquipmentsTreeViewModel(這個用戶控件的datacontext)並保留在CommandParameter我的xxx:ModuleParamXXXViewModel(誰是我們觸發右鍵單擊以顯示contextMenu的樹視圖中的項目)?
  2. 如何用MVVM實踐以其他方式實現此目標?

我還試圖用這一點,但它並沒有太多的工作:

<MenuItem Header="Add new equipment" Command="{Binding Path=DataContext.AddNewEquipmentCommand, Source={x:Reference TreeViewProtocolsAndEquipments}}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}"> 

而與此我得到對象引用不設置到對象的實例

回答

0

UserControl不是MenuItem的可視祖先,因爲ContextMenu駐留在它自己的可視化樹中。

綁定TextBlockUserControl,然後Tag屬性Command屬性綁定到ContextMenuPlacementTarget

<TreeView x:Name="TreeViewProtocolsAndEquipments" AllowDrop="True" 
        ItemsSource="{Binding ModuleParams}"> 
    <TreeView.Resources> 
     <!-- CONTEXT MENU --> 
     <!-- Protocol --> 
     <ContextMenu x:Key="ContextMenuProtocol"> 
      <MenuItem Header="Add new equipment" 
           Command="{Binding PlacementTarget.Tag.DataContext.AddNewEquipmentCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}" 
           CommandParameter="{Binding}"> 
       <MenuItem.Icon> 
        <Image Source="Images/Add.png" /> 
       </MenuItem.Icon> 
      </MenuItem> 
      <Separator /> 
     </ContextMenu> 

     <!-- MODULE XXX --> 
     <!-- ModuleParam > xxx --> 
     <HierarchicalDataTemplate DataType="{x:Type xxx:ModuleParamXXXViewModel}" ItemsSource="{Binding ModuleItems}"> 
      <TextBlock Text="XXX" Foreground="Green" 
           Tag="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}" 
           ContextMenu="{StaticResource ContextMenuProtocol}"/> 
     </HierarchicalDataTemplate> 
    </TreeView.Resources> 
</TreeView> 
+0

感謝你的幫助,它的工作原理,但現在它永遠不會觸發我的ContextMenu,當我右鍵單擊一個菜單項,它不顯示contextMenu,但執行命令...我的綁定在菜單項中,所以,對於我來說,它必須在context菜單中的菜單項上單擊而不是觸發moduleitem在視覺樹中的權利?那麼爲什麼它執行這樣的命令呢? – Karakayn

+0

它的工作原理,但你沒有看到ContextMenu?這恐怕沒有意義。另外,根據我的建議更改綁定不會導致ContextMenu消失,所以我不知道您在做什麼。 – mm8

+0

我只是愚蠢的,我讓我的CommandHandler構造函數中的某個地方_canExecute = true;所以它可以始終執行命令,不再需要點擊...現在它可以工作,但我只是禁用了MenuItem,所以我不能再點擊它...我會嘗試找出發生了什麼事(通過這個visualtree和logicaltree),謝謝你的幫助! – Karakayn