WPF MVVM 中的动态 TabControl

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11625143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 04:41:40  来源:igfitidea点击:

Dynamic TabControl in WPF MVVM

c#wpfxamltabcontroltabitem

提问by Desnoxav

I started using WPF (in C#) some weeks ago and now i need help for some advanced use of tabcontrol.

几周前我开始使用 WPF(在 C# 中),现在我需要一些高级使用 tabcontrol 的帮助。

Fisrt of all, I'm using a MVVM (Model View ViewModel) pattern for designing my application, and I have a constraint that is to try to not add code in code behind file (that inititalise xaml files).

首先,我正在使用 MVVM(模型视图视图模型)模式来设计我的应用程序,并且我有一个约束,即尝试不在代码隐藏文件(即初始化 xaml 文件)中添加代码。

Now my issue is to create dynamically new tabItems in my MainWindow View (Window) that shows an instance of my Detail View (Page) when I click on a button ("New Tab" button for example).

现在我的问题是在我的 MainWindow 视图(窗口)中动态创建新的 tabItems,当我点击一个按钮(例如“新标签”按钮)时,它会显示我的详细信息视图(页面)的一个实例。

I have found a lot off stuff about dynamic creation off tabitem on the web but often with modifications in code behind files. I though using binding but I don't know how can I use binding to this kind of stuff.

我在网上发现了很多关于 tabitem 动态创建的内容,但通常会修改文件背后的代码。我虽然使用绑定,但我不知道如何使用绑定到这种东西。

回答by Mare Infinitus

MVVM will help you out.

MVVM 会帮助你。

Create a ViewModel for your MainWindow View. There you can have a collection of DetailViewModels. Just use an ObservableCollectionof DetailViewModels here.

为您的 MainWindow 视图创建一个 ViewModel。在那里您可以拥有 DetailViewModels 的集合。只需ObservableCollection在此处使用DetailViewModels 。

In your View, bind the ItemsSourceof the TabControlto that Collection.

在你看来,绑定ItemsSourceTabControl该集合。

Your AddTab Button can have a Command Binding. That Command can be a ICommandderived class, that is published in the MainWindowViewModel. Pressing the button then ends up in the MainWindow ViewModel, adding another DetailViewModel and updating the View that way.

您的 AddTab 按钮可以有一个命令绑定。该 Command 可以是ICommand派生类,在 MainWindowViewModel 中发布。按下按钮然后在 MainWindow ViewModel 中结束,添加另一个 DetailViewModel 并以这种方式更新 View。

Have a look to this excellent video tuturial on MVVM here: Jason Dollinger on MVVM

在此处查看有关 MVVM 的优秀视频教程:Jason Dollinger on MVVM

He explains how that can be made, with examples for Main and Detail ViewModel and commands.

他通过 Main 和 Detail ViewModel 和命令的示例解释了如何实现。

The sourcecode that he develops in his video is available here: Sourcecode on Lab49

他在视频中开发的源代码可在此处获得:Lab49 上的源代码

Perhaps it even easier for you to create a ViewModel that is just used by the TabControl. Set the DataContextof the TabControlto that TabControlViewModel then. Publish that TabControlViewModel in your MainWindowViewModel as a public property, to accomplish that.

也许您更容易创建一个仅由TabControl. 设置DataContextTabControl该TabControlViewModel即可。在 MainWindowViewModel 中将该 TabControlViewModel 作为公共属性发布,以实现这一点。

Your code-behind will be empty, except some InitializeComponent perhaps.

您的代码隐藏将是空的,除了一些 InitializeComponent 可能。

回答by vadim

Great setof articles about MVVM by Sacha Barber contains downloadable application where he expose they way of dynamic view creation using tab control. You can download demo application attached to article to see how to use dynamic tab control. Example:

由萨沙理发关于MVVM文章包含下载的应用程序在那里,他揭露使用标签控制他们的动态视图创建方式。您可以下载文章附带的演示应用程序,以了解如何使用动态选项卡控件。例子:

You need where you would bind collection of workspaces that represent data model for tab content and if needed addition properties for tab e.g. closable or not, disabled or not, Name and so on. This can be your ViewModelBase. Create you tab item template to support your view model.

您需要在哪里绑定代表选项卡内容数据模型的工作区集合,如果需要,还需要为选项卡添加属性,例如是否可关闭、是否已禁用、名称等。这可以是您的 ViewModelBase。创建您的选项卡项模板以支持您的视图模型。

<TabControl x:Name="tabControl"
                 IsSynchronizedWithCurrentItem="True" 
                 ItemsSource="{Binding Path=Workspaces}" 
                 Template="{StaticResource DynamicTabControlTemplate}">         
</TabControl>

Init collection of workspaces for example like this

例如像这样初始化工作区集合

Workspaces = new ObservableCollection();

工作区 = new ObservableCollection();

And after you adding new item to collection tab control will be changed too.

在您将新项目添加到集合选项卡后,控件也会发生变化。

回答by Vale

You can bind to ItemsSourceproperty of a TabControlwhich represents collection of tab items. Then you can manipulate the collection from your ViewModel.

您可以绑定到表示选项卡项集合的ItemsSource属性TabControl。然后您可以从您的 ViewModel 操作该集合。