如何将命令绑定到祖先datacontext? WPF :: MVVM

时间:2017-01-22 14:16:06

标签: c# wpf mvvm contextmenu itemtemplate

我在ContextMenu内有一个button和一个TabControl,我让按钮Command正常工作,但无法弄清楚如何绑定Context menu items命令。你能指出我做错了吗?

注意:命令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>

ViewModel代码:

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

2 个答案:

答案 0 :(得分:2)

将DockPanel的Tag属性绑定到视图模型,然后将MenuItem的Command属性绑定到ContextMenu的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。

答案 1 :(得分:0)

您是否尝试将AncestorType绑定到Window或UserControl?

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