WPF: IEditableCollectionView and CanAddNew and empty collections

Posted by Aran Mulholland on Stack Overflow See other posts from Stack Overflow or by Aran Mulholland
Published on 2010-04-21T06:25:15Z Indexed on 2010/04/21 6:33 UTC
Read the original article Hit count: 727

We were having some issues with the wpf datagrid and IEditableCollectionView (although this question applies to using IEditableCollectionView and ItemsControl) When you have a collection with no items in it, the IEditableCollectionView cannot determine what items should be inserted so it sets CanAddNew=false we found a solution here (buried deep in the comments) that goes like so :

If you derive from ObservableCollection like this

public class PersonsList : ObservableCollection<Person> { }

you will find out that if the initial collection is empty, there won't be a NewItemPlaceHolder showing up on the view. That's because PersonsList cannot resolve type T at design time. A workaround that works for me is to pass type T as a parameter into the class like this

PersonsList<T> : ObservableCollection<T> where T : Person { }

This approach will place the NewItemPlaceHolder even if the collection is empty.

I'm wondering if there is an interface i can implement on my collections that inform the IEditableCollectionView which type to create should i get an AddNew request.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about ieditablecollectionview