8,734
社区成员
发帖
与我相关
我的任务
分享
public class ProductsViewModel
{
private NorthwindDomainContext _Context = new NorthwindDomainContext();
private EntityList<Product> _source;
private DomainCollectionViewLoader _loader;
private DomainCollectionView _view;
public ProductsViewModel()
{
_source = new EntityList<Product>(_Context.Products);
_loader = new DomainCollectionViewLoader<Product>(LoadProducts, OnLoadProductsCompleted);
_view = new DomainCollectionView(_loader, _source);
INotifyCollectionChanged notifySortDescriptions = (INotifyCollectionChanged)CollectionView.SortDescriptions;
notifySortDescriptions.CollectionChanged += (s, e) => this._view.MoveToFirstPage();
if (!DesignerProperties.IsInDesignTool)
{
using (this.CollectionView.DeferRefresh())
{
this._view.PageSize = 15;
this._view.MoveToFirstPage();
}
}
}
// ProductEntitySet只用于测试
public EntitySet<Product> ProductEntitySet
{
get { return this._Context.Products; }
}
public ICollectionView CollectionView
{
get
{
return this._view;
}
}
LoadOperation<Product> LoadProducts()
{
var query = _Context.GetProductsQuery();
return _Context.Load(query.SortAndPageBy(this._view));
}
void OnLoadProductsCompleted(LoadOperation<Product> lo)
{
if (lo.HasError)
{
lo.MarkErrorAsHandled();
MessageBox.Show(lo.Error.Message);
}
else if (!lo.IsCanceled)
{
this._source.Source = lo.Entities;
if (lo.TotalEntityCount != -1)
{
this._view.SetTotalItemCount(lo.TotalEntityCount);
}
}
}
}
<sdk:DataGrid ItemsSource="{Binding CollectionView}" Margin="12,12,0,0" Name="productDataGrid" ……>
<sdk:DataPager Source="{Binding CollectionView}" Name="dataPager1" PageSize="10" />