c# - 如何将命令绑定(bind)到祖先数据上下文? WPF::MVVM

标签 c# wpf mvvm contextmenu itemtemplate

我有一个 ContextMenubutton ,在 TabControl 内, 我得到了按钮 Command正常工作,但不知道如何绑定(bind) Context menu items命令。你能指出我做错了什么吗?

备注 : 两个命令 CloseTabCommandCloseAllTabsCommand ,将它们绑定(bind)到按钮时工作正常。

Xml代码:

<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>

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

编辑:

绑定(bind)到 TabItemViewModel 中声明的命令(TabControl ItemsSource) 类工作正常。但我想将命令绑定(bind)到 ViewModel当前UserControl

最佳答案

将 DockPanel 的 Tag 属性绑定(bind)到 View 模型,然后将 MenuItem 的 Command 属性绑定(bind)到 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 来绑定(bind)到父 TabControl,因为在可视化树的更上方没有父 TabControl。

关于c# - 如何将命令绑定(bind)到祖先数据上下文? WPF::MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41791952/

相关文章:

c# - 如何使用 Entity Framework 访问子表

c# - 检查 FrameworkElement 是否有 Border

C#/WPF : Implementing Autosave

ios - RxSwift、MVVM、Alamofire/Moya。使用间隔更新股票代码

c# - autocad .net在不使用ObjectId的情况下存储和检索对象ID

c# - .NET - 根据使用的构造函数显示/隐藏方法?

c# - 在.wav比特率之间转换时降低音量

c# - 在 asp.net mvc 中应该在哪里创建/操作 View 模型?

c# - WPF 底部的 TabControl 选项卡

mvvm - 滚动时Recyclerview抽搐