多个绑定ViewModel以查看MVVM

时间:2013-08-26 19:42:20

标签: wpf xaml windows-phone-7 data-binding mvvm

请帮我理解。我有两个PanoramaPage.xaml的视图PanoramaItem。第一项是来自某个Web服务的新闻列表,第二项是该服务的用户列表。新闻和用户不同Models

查看:

<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 一个xaml PanoramaPage.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
    }

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 
         }

您怎么看?

1 个答案:

答案 0 :(得分:3)

单一ViewModel。并非每个视图控件都有自己的视图模型。您将ViewModel设置为整个视图的DataContext。 即使你使用两个viewmodel,你也需要拥有父视图模型,它将包含这两个视图模型的实例。此父视图模型将充当整个视图的Datacontext,子控件将其datacontext设置为这些子视图模型,因此您还必须更改绑定。

但单视图单视图模型就是mvvm。

由于