2017-01-22 85 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