DomainCollectionView 和 WCF RIA 服务中的关联属性
我的 poco 类中有一个关联,例如:
public class Category() {
[Key]
public int id { get; set}
public string Name { get; set; }
/* HERE */
public virtual ICollection<Book> Books {get; set;}
}
public class Book() {
[Key]
public int id { get; set}
public string Name { get; set; }
}
我使用 MVVM 模式、MVVM Light 和 RIA Services Toolkit。我的域服务实现包含一个 GetCategories 方法,其中包含他们的书籍,例如:
public IQueriable<Category> GetCategories()
{
return Model.Categories.Include("Books").OrderBy(pCategory => pCategory.Name);
}
在我的 ViewModel 中,我有一个加载 GetGruposQuery 的 DomainCollectionView。我还有一个用于绑定网格和其他控件的属性,例如:
public ICollectionView CollectionViewCategories {
get { return myDomainCollectionViewCategories;}
}
我需要获取一个子属性 CollectionView.Books 来绑定我的控件并在视图中添加、删除 itens,但此属性只是 EntityCollection,而不是包含的 DomainCollectionView ADD、REMOVE 等方法。
如何在 ViewModel 中获取当前 Books 属性(CollectionViewCategories)作为 DomainCollectionView?
谢谢你!
我用以下方法解决这个问题:(CollectionViewCategories.CurrentItem as Category).Books.Remove(CollectionViewBooks.CurrentItem as Book)
private ICollectionView CreateView(Object source)
{
CollectionViewSource cvs = new CollectionViewSource();
cvs.Source = source;
return cvs.View;
}
//...
//After CollectionViewCategories loaded:
CollectionViewCategories.CurrentChanged += (s, e) =>
{
if (CollectionViewCategories.CurrentItem != null)
{
CollectionViewBooks = CreateView(fContext.Categories.Where(p => p.Id == (CollectionViewCategories.CurrentItem as Category).Id).FirstOrDefault().Books);
}
else
{
CollectionViewBooks = null;
}
RaisePropertyChanged("CollectionViewBooks");
};
I have a association in my poco class, ex:
public class Category() {
[Key]
public int id { get; set}
public string Name { get; set; }
/* HERE */
public virtual ICollection<Book> Books {get; set;}
}
public class Book() {
[Key]
public int id { get; set}
public string Name { get; set; }
}
I use MVVM patern, MVVM Light and RIA Services Toolkit. My Domain Service implementation contains a method GetCategories that include their books, ex:
public IQueriable<Category> GetCategories()
{
return Model.Categories.Include("Books").OrderBy(pCategory => pCategory.Name);
}
In my ViewModel I have a DomainCollectionView that load GetGruposQuery. I also have a property for bind a grid and other controls, like:
public ICollectionView CollectionViewCategories {
get { return myDomainCollectionViewCategories;}
}
I need get a child property CollectionView.Books for bind my controls and ADD, REMOVE itens in view, but this property is only EntityCollection and isn't a DomainCollectionView that contains methods for ADD, REMOVE, etc.
How I can get the current Books property (of CollectionViewCategories) as DomainCollectionView in my ViewModel?
Thank you!
I solve this question with: (CollectionViewCategories.CurrentItem as Category).Books.Remove(CollectionViewBooks.CurrentItem as Book)
private ICollectionView CreateView(Object source)
{
CollectionViewSource cvs = new CollectionViewSource();
cvs.Source = source;
return cvs.View;
}
//...
//After CollectionViewCategories loaded:
CollectionViewCategories.CurrentChanged += (s, e) =>
{
if (CollectionViewCategories.CurrentItem != null)
{
CollectionViewBooks = CreateView(fContext.Categories.Where(p => p.Id == (CollectionViewCategories.CurrentItem as Category).Id).FirstOrDefault().Books);
}
else
{
CollectionViewBooks = null;
}
RaisePropertyChanged("CollectionViewBooks");
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么需要它? EntityCollection 确实有这些方法(添加、删除...)
如果在调用它们时出现错误,可能会导致 Book 类未完全暴露给客户端(域服务上缺少插入/更新/删除方法)
澄清一下,DomainCollectionView 旨在包装 EntityCollection 主要是为了更好地处理 MVVM 和一般绑定(请参阅此 链接)
Why do you need it ? EntityCollection do have these methods (Add,Remove...)
If you obtain an error while invoking them, it could lead to the fact that the Book class is not fully exposed to the client (lack of insert/update/delete methods on the domainservice)
Just to clarify, DomainCollectionView is inteded to wrap an EntityCollection primarily for better dealing with MVVM and binding in general (see this link)