2017-01-22 93 views
0

我有一個ContextMenu和一個button,在TabControl,我得到按鈕Command工作正常,但不知道如何綁定Context menu items命令。你能指出我做錯了什麼嗎?如何將命令綁定到祖先datacontext? WPF :: MVVM

注意:命令CloseTabCommandCloseAllTabsCommand,都綁定到按鈕時正常工作。

XAML代碼:

<TabControl ItemsSource="{Binding TabItems}"> 
       <TabControl.ItemTemplate> 
        <DataTemplate> 
         <DockPanel Width="120" ToolTip="{Binding HeaderText}"> 
          <DockPanel.ContextMenu> 
           <ContextMenu> 
            <MenuItem Header="Close Tab" 
               Command="{Binding DataContext.CloseTabCommand, RelativeSource={RelativeSource AncestorType=TabControl}}" 
               CommandParameter="{Binding ItemId}" /> 
            <MenuItem Header="Close All Tabs" 
               Command="{Binding DataContext.CloseAllTabsCommand, RelativeSource={RelativeSource AncestorType=TabControl}}" /> 
           </ContextMenu> 
          </DockPanel.ContextMenu> 
          <Button 
           Command="{Binding DataContext.CloseTabCommand, RelativeSource={RelativeSource AncestorType=TabControl}}" 
           CommandParameter="{Binding ItemId}" 
           Content="X" 
           Cursor="Hand" 
           DockPanel.Dock="Right" 
           Focusable="False" 
           FontFamily="Courier" 
           FontWeight="Bold" 
           FontSize="10" 
           VerticalContentAlignment="Center" 
           Width="15" Height="15" /> 
          <ContentPresenter Content="{Binding HeaderText}" VerticalAlignment="Center" /> 
         </DockPanel> 
        </DataTemplate> 
       </TabControl.ItemTemplate> 
       <TabControl.ItemContainerStyle> 
        <Style TargetType="TabItem"> 
         <Setter Property="IsSelected" Value="{Binding IsSelected}" /> 
        </Style> 
       </TabControl.ItemContainerStyle> 
      </TabControl> 

視圖模型代碼:

private ObservableCollection<TabItemViewModel> _tabItems; 
     public ObservableCollection<TabItemViewModel> TabItems { 
      // if _tabItems is null initiate object. 
      get { return _tabItems; } 
      set { SetProperty(ref _tabItems, value); } 
     } 

編輯:

綁定到司令部TabItemViewModel(的TabControl的ItemsSource)類中聲明的正常工作。但我想要綁定命令ViewModel目前UserControl

+1

你看這個嗎? http://stackoverflow.com/questions/15033522/wpf-contextmenu-woes-how-do-i-set-the-datacontext-of-the-contextmenu – blins

回答

1

綁定DockPanel中的向視圖模型的Tag屬性,然後將菜單項的命令屬性綁定到文本菜單的PlacementTarget:

<DockPanel Width="120" ToolTip="{Binding HeaderText}" 
      Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=TabControl}}"> 
    <DockPanel.ContextMenu> 
     <ContextMenu> 
      <MenuItem Header="Close Tab" 
         Command="{Binding PlacementTarget.Tag.CloseTabCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}" 
         CommandParameter="{Binding ItemId}" /> 
      ... 

一個ContextMenu駐留在自己的視覺樹這就是爲什麼你不能使用RelativeSource綁定到父級TabControl,因爲在可視樹中沒有父級TabControl。

+0

感謝您的解釋爲什麼它沒有工作:) – IBRA

0

您是否嘗試綁定您的AncestorType到窗口或UserControl?

Command="{Binding CloseTabCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" 
+0

我有,它沒有工作。不知道爲什麼? – IBRA

+0

嘗試從DataContext.CloseTabCommand中移除DataContext –

+0

我嘗試了使用和不使用DataContext,在這兩個命令上,我試着移動按鈕代碼中的ContextMenu,僅用於測試。它也沒有工作。只是爲了澄清Button命令他們都工作,但當使用它沒有工作 – IBRA