I'm trying to do something rather simple, and getting frustrated by .Net:
I simply want to have a WPF DataGrid that automatically adds a blank row at the end, similar to entering data in a table in MS Access or SQL Management Studio.
My idea was to override ObservableCollection: keep a newItem member, which I dynamically add when the Items collection is read. Something like this:
// Warning this code doesn't work
public class MyCollection<T> : ObservableCollection<T>
{
private T _newItem;
// there would be a constructor which would initialize _newItem
protected override IList<T> Items
{
get
{
List<T> newItems = new List<T>();
newItems.Add(_newItem);
return base.Items.Union(newItems).ToList();
}
}
}
There are several problems with my code. First, you can't override the getter of the Items property. It isn't overridable.
Second, even if you could, that getter isn't even called when a DataGrid is bound to the collection. I'm thinking the GetEnumerator method should be overridden here, but of course that's not overridable either.
What's the easiest way to extend the ObservableCollection class to implement this functionality. At this point it's looking like I'll have to implement the whole thing myself, and not inherit from ObservableCollection. For obvious reasons I'm resistant to that idea.
Thanks in advance!