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
}
您怎麼看?
謝謝@nit!一個令人信服的答案。 – Alexandr