WPF: IEditableCollectionView and CanAddNew and empty collections
- by Aran Mulholland
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.