2011-04-20 77 views
1

如标题所示,我不想把它全部放在一个ViewModel中,我想每个TabItem都有一个viewModel,但是我对WPF和MVVM是新的,所以请裸露跟我。如何将不同的TabItems绑定到不同的ViewModels

我创建用于包含TabControl的,其中I具有这个特性currentViewModel窗口一个mainViewModel,指出为默认值的mainViewModel构造

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

当在另一个TabItem的用户点击这个执行

currentViewModel = "viewModel2"; 

当然set访问具有onPropertyChanged方法

public String currentViewModel 
    { 
     get { return _currentViewModel; } 

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

另两个viewModels(viewModel1,viewModel2)每一个都决定了我想要在其中切换的一个tabItems的功能。

现在在我的Main.xaml我想绑定我的dataContext首先是MainViewModel,然后到currentViewModel属性。所以无论用户何时点击一个tabItem,currentViewModel属性都会更新,并且dataContext指向相应的视图模型。我希望这是提前足够清晰

感谢

+0

使用字符串设置viewmodels? Yuck,我不认为这是必要的。另外一个人通常会在setter中的这两行周围有一个'if(_fieldOfProperty!= value)'块。 – 2011-04-21 01:01:26

回答

2

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

您的MainWindow将有tabcontrol,并且tabcontrol中的每个tabitem都将绑定到特定的usercontrol(它被视为一个单独的视图)。

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

如果您需要的代码示例,让我知道。

+0

谢谢,解决了我的问题 – Musaab 2011-04-21 09:59:00

+0

您能否给我提供任何代码示例。 Thnx提前。 – 2014-03-30 14:36:21

2

我怀疑这是你真正想做的事,如果你有不同的ViewModels您可以将您的控制仍然绑定到这些的ViewModels的集合,模板它们作为必要。

你只需要创建在不同层次不同的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)以获取ViewModels的正确视图。