如何将不同的tabItem绑定到不同的ViewModel

时间:2011-04-20 22:41:47

标签: .net wpf mvvm

正如标题所示,我不想把它全部放在一个ViewModel中,我希望每个tabItem有一个viewModel,但我是WPF和MVVM的新人,所以请光临我。

我为包含TabControl的窗口创建了一个mainViewModel,其中我有这个属性currentViewModel,指向mainViewModel构造函数的默认值

    public MainViewModel()
    {
        currentViewModel = "viewModel1";
    }

当用户点击另一个tabItem时,执行

currentViewModel = "viewModel2";

当然set访问器具有onPropertyChanged方法

    public String currentViewModel
    {
        get { return _currentViewModel; }

        set
        { 
            _currentViewModel = value;
            OnPropertyChanged("currentViewModel");
        }
    }

另外两个viewModel(viewModel1,viewModel2)每个都确定我要切换的tabItem之一的功能。

现在在我的Main.xaml上我想将我的dataContext首先绑定到MainViewModel,然后绑定到currentViewModel属性。这样每当用户点击tabItem时,currentViewModel属性都会更新,dataContext指向相应的viewmodel。我希望这很清楚

提前致谢

2 个答案:

答案 0 :(得分:2)

我怀疑这是你真正想做的事情,如果你有不同的ViewModel,你仍然可以将你的控件绑定到那些ViewModel的集合并根据需要对它们进行模板化。

你只需要在不同的级别创建不同的DataTemplates,这是一个例子(它直接使用模型但现在不重要):

<TabControl>
    <TabControl.ItemsSource>
        <!-- This is just sample data, normally you would bind this to an
             ObservableCollection<ViewModelBase> or something similar -->
        <x:Array Type="{x:Type sys:Object}">
            <local:Employee Name="John" Occupation="Programmer"/>
            <local:Employee Name="Steve" Occupation="Coffee Getter"/>
            <local:Machine Manufacturer="iCorp" Model="iMachine"/>
            <local:Machine Manufacturer="iCorp" Model="iMachine G2"/>
            <local:Employee Name="Marc" Occupation="GUI Designer"/>
        </x:Array>
    </TabControl.ItemsSource>
    <TabControl.Resources>
        <!-- These datatemplates define the tab-header appearance, by placing them
             in the TabControl.Resources and setting the DataType they get applied
             automatically, just make one light-weight template for each ViewModel -->
        <DataTemplate DataType="{x:Type local:Employee}">
            <TextBlock>
                <Run Text="{Binding Name}"/>
                <Run Text="("/>
                <Run Text="{Binding Occupation}"/>
                <Run Text=")"/>
            </TextBlock>
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:Machine}">
            <TextBlock>
                <Run Text="{Binding Manufacturer}"/>
                <Run Text=" - "/>
                <Run Text="{Binding Model}"/>
            </TextBlock>
        </DataTemplate>

        <ContentControl x:Key="MainContent" Content="{Binding}">
            <ContentControl.Resources>
                <!-- This represents the content of the TabItems, you probably
                     do not need to create DataTemplates but since you could
                     use a View of the ViewModels instead -->
                <DataTemplate DataType="{x:Type local:Employee}">
                    <StackPanel>
                        <TextBlock Text="{Binding Name}"/>
                        <TextBlock Text="{Binding Occupation}"/>
                        <TextBlock Text="{Binding Id}"/>
                        <TextBlock Text="{Binding IsActive}"/>
                    </StackPanel>
                </DataTemplate>
                <DataTemplate DataType="{x:Type local:Machine}">
                    <StackPanel>
                        <TextBlock Text="{Binding Manufacturer}"/>
                        <TextBlock Text="{Binding Model}"/>
                        <TextBlock Text="{Binding VintageYear}"/>
                        <TextBlock Text="{Binding Price}"/>
                    </StackPanel>
                </DataTemplate>
            </ContentControl.Resources>
        </ContentControl>
    </TabControl.Resources>
    <TabControl.ContentTemplate> <!-- Setting the content to the resource -->
        <DataTemplate>
            <Border>
                <StaticResource ResourceKey="MainContent"/>
            </Border>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

我可能会尝试获取MVVM的具体细节,但希望这已经让我们知道如何处理它。

您可能需要设置模板选择器(TabControl.ContentTemplateSelector)以获取ViewModel的正确视图。

答案 1 :(得分:2)

我认为一个好的方法是为每个标签项创建用户控件。然后,在每个用户控件中,您将引用要绑定的正确viewModel命名空间。

您的MainWindow将具有tabcontrol,tabcontrol中的每个tabitem将绑定到特定的usercontrol(被视为单独的View)。

mainwindow_View.xaml将绑定到mainwindow_ViewModel.cs tabItem1_View.xaml将绑定到ViewModel1.cs tabItem2_View.xaml将绑定到ViewModel2.cs

如果您需要代码示例,请与我们联系。

相关问题