“MVVM”中的“ViewModel”
在“Silverlight”的项目中,其中有一个“ViewModel.cs”类。
1、“ViewModel.cs”类中有一个“DataPointsOfSeries”(实体对象),代码如下:
public ObservableCollection<DataPointCollection> DataPointsOfSeries
{
get
{
return _dataPointsOfSeries;
}
set
{
_dataPointsOfSeries = value;
FirePropertyChanged("DataPointsOfSeries");
}
}
2、在“XAML”(Page.xaml)文件中
<UserControl.DataContext>
<local:ViewModel></local:ViewModel>
</UserControl.DataContext>
<vc:DataSeries RenderAs="Line" LightWeight="true" ShadowEnabled="false" LightingEnabled="false" MarkerSize="4" LineThickness="1" DataPoints="{Binding DataPointsOfSeries[0]}" />
问题一:通过“<UserControl.DataContext>”,“ViewModel.cs”类仿佛变成了“Page.xaml文件的后台文件(Page.xaml.cs)”,其中的“DataPointsOfSeries”实体对象可以直接被绑定,请问这是为什么???
问题二:“<UserControl.DataContext>”到底有什么作用呢?是不是“DataPointsOfSeries”实体对象如果在“Page.xaml.cs”中就不需要“<UserControl.DataContext>”了???
问题三:“Page.xaml.cs”中几乎没有逻辑了,全部被封装在了“ViewModel.cs”类中。从“Page.xaml.cs”调用“ViewModel.cs”类中方法,“<UserControl.DataContext>”就好像是一个桥梁似的?