使用 Prism - WPF 关闭动态添加的标签项 [英] Close dynamically added Tab Items using Prism - WPF

查看:92
本文介绍了使用 Prism - WPF 关闭动态添加的标签项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Prism RegionManager,通过 MainView 内的 TabControl 区域注册不同的视图.

I'm using Prism RegionManager, to register different views with a TabControl region inside the MainView.

主视图.xaml

    <TabControl regions:RegionManager.RegionName="MainViewTabRegion">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <DockPanel Width="Auto">
                    <Button Command="{Binding DataContext.DataContext.CloseTabCommand, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}"
                            CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}"
                            Content="X"
                            Cursor="Hand"
                            DockPanel.Dock="Right"
                            Focusable="False"
                            FontFamily="Courier"
                            FontWeight="Bold"
                            Margin="4,0,0,0"
                            FontSize="10"
                            VerticalContentAlignment="Center"
                            Width="15" Height="15" />

                            <ContentPresenter Content="{Binding DataContext.DataContext.HeaderText, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}" />
                 </DockPanel>
             </DataTemplate>
        </TabControl.ItemTemplate>
    </TabControl>

在 MainViewViewModel 中,我添加了具有相同基类的不同视图.

In MainViewViewModel I'm Adding different views with the same base class.

MainViewViewModel.cs:

MainViewViewModel.cs:

private void AddProjectView() {
     var view = _container.Resolve<ProjectSettingsView>();
     var dataContext = _container.Resolve<ProjectSettingsViewModel>();
     dataContext.HeaderText = "test header txt";
     view.DataContext = dataContext;
     _regionManager.RegisterViewWithRegion("MainViewTabRegion", () => view);
}

我可以在视图中添加新的标签项.

I can add new tab item with the view.

如何关闭标签项,上面XAML代码中的,在ProjectSettingsViewModel中添加了一个带有CloseCommand的关闭按钮,与 TabItem 绑定.

How can I close the tab item, the <TabControl.ItemTemplate> in the XAML code above, adds a close button with CloseCommand in the ProjectSettingsViewModel, with the TabItem bonded to it.

ProjectSettingsViewModel.cs

ProjectSettingsViewModel.cs

private void OnExecuteCloseCommand(object tabItem) {
     //Close this TabItem
}

推荐答案

ButtonCommandParameter 属性绑定到 DataContextTabItem:

Bind the CommandParameter property of the Button to the DataContext of the TabItem:

<Button Command="{Binding DataContext.DataContext.CloseTabCommand, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}"
                            CommandParameter="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}"
                            Content="X"
                            Cursor="Hand"
                            DockPanel.Dock="Right"
                            Focusable="False"
                            FontFamily="Courier"
                            FontWeight="Bold"
                            Margin="4,0,0,0"
                            FontSize="10"
                            VerticalContentAlignment="Center"
                            Width="15" Height="15" />

然后您可以在视图模型中像这样删除视图:

You could then remove the view like this in the view model:

public class ProjectSettingsViewModel
{
    private readonly IRegionManager _regionManager;

    public ProjectSettingsViewModel(IRegionManager regionManager)
    {
        _regionManager = regionManager;
        CloseTabCommand = new DelegateCommand<object>(OnExecuteCloseCommand);
    }

    private void OnExecuteCloseCommand(object tabItem)
    {
        _regionManager.Regions["MainViewTabRegion"].Remove(tabItem);
    }

    public DelegateCommand<object> CloseTabCommand { get; }
}

这篇关于使用 Prism - WPF 关闭动态添加的标签项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆