绑定的TabControl到的ObservableCollection不同的内容

问题描述:

M试图创建W¯¯主窗口,将有包含多个项目一个TabControl(每一个显示仅在需要时)......假设我们已经和C型的A类项目,B类项目项
我想(使用MVVM模式),以有重新present我的TabItems并与相关的我的userscontrols对象的一个​​ObservableCollection(每个TabItem的是一个用户控件)...
问题是,我不知道如何做到这一点。

'm trying to create w mainwindow that will have a tabcontrol containing multiple items ( each one is shown only on demand )... let's say we have item of type A, item of type B and item of type C I want ( using MVVM pattern) to have an observablecollection of objects that represent my tabitems and that are related to my userscontrols ( each tabitem is a usercontrol)... The problem is that i didn't figure out how to do it.

我有一个tabItemViewModelBase类:

i've a tabItemViewModelBase class :

 public class TabItemViewModelBase : ViewModelBase
    {

        //Fields : 
        RelayCommand _closeCommand;


        //Constructor:
        public TabViewModel(string header)
        {
            this.Header = header;
        }

}

在我的主窗口数据的背景下,我这个类的一个观察的集合:

in my mainwindow data context, i've an observable collection of this class :

 //Propriétés
        ObservableCollection<TabViewModel> _tabItems;

在我的主窗口,我下面的标签的TabControl的项目

In my MainWindow i've the following Tag for the TabControl Item

 <TabControl Padding="0" ItemsSource="{Binding Path=Workspaces}">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Header}"/>
                </DataTemplate>
            </TabControl.ItemTemplate>
            <TabControl.ContentTemplate>
                <DataTemplate>
                    **<view:ClientView/>**
                </DataTemplate>
            </TabControl.ContentTemplate>

        </TabControl>

但你可能会看到,所有项目都连接到ClientView用户控制和我使用tabviewitem创建我的项目,我需要一个财产或指定的ObservableCollection的各个元素的内容形式的方式...

but as you may see, all the items are attached to ClientView User control and i'm using tabviewitem to create my items, i need a property or a way to specify the content form for each element of the observablecollection...

(我有一个ClientListingViewModel类和ClientCreationViewModel类),我不能同时使用,因为我不知道如何指定为他们每个人的看法!

( i've a ClientListingViewModel Class , and ClientCreationViewModel class) , and i can't use both because i don't know how to specify the view for each of them!

谢谢!

要指定针对特定的ViewModels的意见,你可以在一个DataTemplate做到这一点。

To specify views that target specific viewmodels, you can do this in a datatemplate.

首先,你需要一个命名空间引用添加到您的视图,视图模型命名空间。我已经包括使用窗口的例子,但它也适用于用户控件。

First you need to add a namespace reference to your view and viewmodel namespaces. I've included an example using a Window, but it also works for UserControls.

<Window ...
        xmlns:v="clr-namespace:PutYourViewNamespaceHere"
        xmlns:vm="clr-namespace:PutYourViewModelNamespaceHere">

接下来,您将需要在容器的资源部分定义的DataTemplates。
在下面的例子中,我使用的ClientListingView作为DataTemplate中的ClientListingViewModel

Next you will need to define the datatemplates in the resources section of your container. In the example below, I'm using the ClientListingView as the datatemplate for ClientListingViewModel

<Window.Resources>
     <DataTemplate DataType="{x:Type vm:ClientListingViewModel">
          <v:ClientListingView />
     </DataTemplate>
     <DataTemplate DataType="{x:Type vm:ClientCreationViewModel">
          <v:ClientCreationView />
     </DataTemplate>
</Window.Resources>