TabControl的每个选项卡上的不同视图/用户控件

问题描述:

我正在尝试编写一个使用选项卡保存不同用户控件的程序.我当前要发生的是用户单击查找"按钮,创建了一个新选项卡,并在其中出现了查找屏幕.用户可以使用查找"屏幕选择客户端,然后在它们自己的新选项卡中打开这些客户端,以便用户对其进行编辑.因此,如果用户进入并选择了三个客户端,则该屏幕将具有四个选项卡,其中一个用于查找屏幕,而三个则位于客户端选项卡.当用户单击该选项卡中用户控件上的退出按钮时,还需要关闭该选项卡.

I'm trying to write a program that uses tabs to hold different usercontrols. What I currently want to happen is the user clicks a find button, a new tab is created, and a find screen appears inside it. Using the find screen the user can select clients and these then open in their own new tabs allowing the user to edit them. So if the user went in and selected three clients, the screen would have four tabs, one for the find screen and three client tabs. It will also need to close the tab when the user clicks an exit button on the usercontrol in that tab.

我的问题是我不确定如何在程序中进行设置.我创建了一个TabControl,并将ItemsSource绑定到一个视图模型集合(只要用户添加新屏幕,就可以添加到该视图模型中).我可以使用DataTemplateSelector选择包含正确视图的DataTemplate,但是我不知道如何将视图资源设置为我的viewmodel.

My problem is that I'm not sure how to set this up in my program. I've created a TabControl and bound the ItemsSource to a collection of viewmodels (that I can add to whenever a user adds a new screen). I can use a DataTemplateSelector to select the DataTemplate that contains the right view, but I don't know how to set the resource of the view to my viewmodel.

我正在WPF中进行此操作,目前正在使用Bxf将视图模型放入视图中,这通常可以正常工作,但是我不确定它是否适合TabControl.

I'm doing this in WPF and I'm currently using Bxf to put my viewmodels into the views, and this normally works but I'm unsure how it fits in with the TabControl.

我试图坚持使用MVVM,所以在我的视图模型中没有视图列表.

I'm trying to stick to MVVM so having a list of views in my viewmodel is out.

以前有人做过类似的事情吗?

Has anyone done something similar to this before?

我刚刚回答了我自己的问题.

I've just answered my own question.

动态创建的标签项是使用tabcontrols itemsource属性中单个项目的数据上下文设置的,在本例中,这是我的视图模型之一.

The tabitems that get created dynamically are set up with a datacontext of the individual item from the tabcontrols itemsource property, in this case one of my viewmodels.

我正确使用的数据模板为viewmodel类型选择了正确的视图并显示该视图.

The datatemplate I used correctly picks up the correct view for the viewmodel type and displays this.

但是,我的视图将视图上网格的数据上下文设置为我的资源,因此没有任何显示.我将其更改为使用数据上下文而不是资源,现在一切正常.

However my view set the datacontext of the grid on the view to my resource and so nothing was showing up. I've changed this to use the datacontext instead of the resource and now everything is working.

所以我的主要问题是使我的视图消耗资源而不是数据上下文.我仍然更喜欢使用资源,但是随着datacontext的工作,我将不得不使用它.

So my main problem was having my views run off of resources and not the datacontext. I still would prefer to use resources but as datacontext works I'm going to have to go with that.