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");
};