TabControl 的每个选项卡上的不同视图/用户控件 [英] Different views/usercontrols on each tab of a TabControl

查看:20
本文介绍了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,但是我不知道如何将视图的资源设置为我的视图模型.

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.

动态创建的 tabitems 设置了来自 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.

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

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.

这篇关于TabControl 的每个选项卡上的不同视图/用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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