2011-04-20 22 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的正確視圖。

相關問題