8,734
社区成员
发帖
与我相关
我的任务
分享
public class EntityList<T> : ObservableCollection<T> where T : global::System.ServiceModel.DomainServices.Client.Entity
public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged
public sealed class EntitySet<TEntity> : EntitySet, IEnumerable<TEntity>, IEnumerable, ICollectionViewFactory where TEntity : global::System.ServiceModel.DomainServices.Client.Entity
public abstract class EntitySet : IEnumerable, INotifyCollectionChanged, IRevertibleChangeTracking, IChangeTracking, INotifyPropertyChanged
public EntityList(EntitySet<T> entitySet);
//
// Summary:
// Initializes a new instance of the Microsoft.Windows.Data.DomainServices.EntityList<T>
// with the specified source
//
// Parameters:
// entitySet:
// The System.ServiceModel.DomainServices.Client.EntitySet<TEntity> that backs
// this list. All items added or removed from this list will also be added or
// removed from the backing Microsoft.Windows.Data.DomainServices.EntityList<T>.EntitySet.
//
// source:
// The source collection used to populate this list
public EntityList(EntitySet<T> entitySet, IEnumerable<T> source);
//ViewModel
public class ProductsViewModel : NotificationObject
{
private int id = 1;
private NorthwindDomainContext _Context = new NorthwindDomainContext();
public ProductsViewModel()
{
if (!DesignerProperties.IsInDesignTool)
{
IsBusy = true;
EntityQuery<Product> query = _Context.GetProductsQuery();
query = from p in query
where p.ProductID == id || p.ProductID == (id + 1)
select p;
_Context.Load(query, LoadBehavior.MergeIntoCurrent, OnLoadProductsCompleted, null);
}
}
public ICommand LoadMoreProducts
{
get
{
return new DelegateCommand(OnLoadMoreProducts);
}
}
// 左侧DataGrid绑定EntitySet<Product>
public EntitySet<Product> Products
{
get { return _Context.Products; }
}
private EntityList<Product> _productsList;
// 右侧DataGrid绑定EntityList<Product>
public EntityList<Product> ProductsList
{
get
{
if (_productsList == null)
_productsList = new EntityList<Product>(Products);
return _productsList;
}
}
void OnLoadMoreProducts()
{
IsBusy = true;
EntityQuery<Product> query = _Context.GetProductsQuery();
query = from p in query
where p.ProductID == id || p.ProductID == (id + 1)
select p;
_Context.Load(query, LoadBehavior.MergeIntoCurrent, OnLoadProductsCompleted, null);
}
private bool _isBusy = false;
public bool IsBusy
{
get
{
return _isBusy;
}
set
{
if (_isBusy != value)
{
_isBusy = value;
RaisePropertyChanged(() => this.IsBusy);
}
}
}
void OnLoadProductsCompleted(LoadOperation<Product> lo)
{
IsBusy = false;
if (lo.HasError)
{
MessageBox.Show(lo.Error.Message);
lo.MarkErrorAsHandled();
}
else if (!lo.IsCanceled)
{
id++;
ProductsList.Source = lo.Entities;
}
}
}
public class ProductsViewModel : NotificationObject
{
private NorthwindDomainContext _Context = new NorthwindDomainContext();
public ICommand LoadMoreProducts
{
get
{
return new DelegateCommand(OnLoadMoreProducts);
}
}
public ICommand SaveChanges
{
get
{
return new DelegateCommand(OnSave);
}
}
void OnSave()
{
_Context.SubmitChanges();
}
void OnLoadMoreProducts()
{
//_Context.Products.Clear();
EntityQuery<Product> query = _Context.GetProductsQuery();
//query = query.Where(p => p.ProductID == 3 || p.ProductID == 4);
query = from p in query
where p.ProductID == 1 || p.ProductID == 3
select p;
_Context.Load(query, LoadBehavior.MergeIntoCurrent, OnLoadProductsCompleted, null);
}
//当界面控件绑定EntityList<Products>时,执行LoadMoreProducts命令的结果是
//ProductID为1、2、3的三个记录。
public EntitySet<Product> Products
{
get
{
return _Context.Products;
}
}
//当界面控件绑定EntityList<Product>时,执行LoadMoreProducts命令的结果是
//ProductID为1、3的二条记录。
public EntityList<Product> _productList;
public EntityList<Product> ProductList
{
get
{
if(_productList == null)
_productList = new EntityList<Product>(Products);
return _productList;
}
}
public ProductsViewModel()
{
if (!DesignerProperties.IsInDesignTool)
{
EntityQuery<Product> query = _Context.GetProductsQuery();
query = query.Where(p => p.ProductID == 1 || p.ProductID == 2);
_Context.Load(query, LoadBehavior.RefreshCurrent, OnLoadProductsCompleted, null);
}
}
void OnLoadProductsCompleted(LoadOperation<Product> lo)
{
if (lo.HasError)
{
MessageBox.Show(lo.Error.Message);
lo.MarkErrorAsHandled();
}
else if (!lo.IsCanceled)
{
ProductList.Source = lo.Entities;
}
}
}