2013-08-26 85 views
1

請幫我理解。我有一個查看PanoramaPage.xaml與兩個PanoramaItem。第一個項目是來自某個Web服務的新聞列表,第二個項目是該用戶的服務列表。新聞和用戶不同Models多個綁定ViewModel查看MVVM

查看:

<controls:PanoramaItem Header="users"> 
       <ListBox Margin="0,0,-12,0"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Orientation="Horizontal" Margin="0,0,0,17">                
           <StackPanel Width="311">          
            <TextBlock Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/> 
            <TextBlock Text="{Binding Aboutself}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/> 
           </StackPanel> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
      </controls:PanoramaItem> 
     </controls:Panorama> 

<controls:PanoramaItem Header="news">    
        <ListBox Margin="0,0,-12,0"> 
         <ListBox.ItemTemplate> 
          <DataTemplate> 
           <StackPanel Margin="0,0,0,17" Width="432" Height="78"> 
            <TextBlock Text="{Binding Title}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/> 
            <TextBlock Text="{Binding Content}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/> 
           </StackPanel> 
          </DataTemplate> 
         </ListBox.ItemTemplate> 
        </ListBox> 
       </controls:PanoramaItem> 

MVVM我應該有 ViewModel的爲兩個控件新聞ListBox和用戶ListBox一個ViewModel一個XAMLPanoramaPage.xaml

PanoramaPageViewModel

public class PanoramaPageViewModel : INotifyPropertyChanged 
    { 
     private ObservableCollection<User> userDataSource; 
     private ObservableCollection<News> newsDataSource; 

     public ObservableCollection<User> UserDataSource 
     { 
      get 
      { 
       if (this.userDataSource == null) 
       { 
        this.userDataSource = new ObservableCollection<User>(); 
       } 
       return this.userDataSource; 
      } 
     } 

     public ObservableCollection<News> NewsDataSource 
     { 
      get 
      { 
       if (this.newsDataSource == null) 
       { 
        this.newsDataSource = new ObservableCollection<News>(); 
       } 
       return this.newsDataSource; 
      } 
     } 

     // LoadUsers(), LoadNews(), etc 
    } 

OR

UsersViewModel

public class UsersViewModel : INotifyPropertyChanged 
     { 
      private ObservableCollection<User> userDataSource;   

      public ObservableCollection<User> UserDataSource 
      { 
       get 
       { 
        if (this.userDataSource == null) 
        { 
         this.userDataSource = new ObservableCollection<User>(); 
        } 
        return this.userDataSource; 
       } 
      } 
      //LoadUsers() etc 
     } 

NewsViewModel

public class NewsViewModel : INotifyPropertyChanged 
     { 
      private ObservableCollection<News> newsDataSource;   

      public ObservableCollection<News> NewsDataSource 
      { 
       get 
       { 
        if (this.newsDataSource == null) 
        { 
         this.newsDataSource = new ObservableCollection<News>(); 
        } 
        return this.newsDataSource; 
       } 
      } 
      //LoadNews() etc 
     } 

您怎麼看?

回答

3

Single ViewModel。並非每個視圖控制都有其自己的視圖模型。您將ViewModel設置爲整個視圖的DataContext。 即使你使用兩個視圖模型,你也需要有父視圖模型,它將包含這兩個視圖模型的實例。這個父視圖模型將作爲整個視圖的Datacontext,並且子控件將它們的datacontext設置爲這些子視圖模型,因此您還必須更改綁定。

但是單視圖單視圖模型就是mvvm。

謝謝

+0

謝謝@nit!一個令人信服的答案。 – Alexandr