I\'m using the WPF DataGrid
(.Net 3.5 SP 1 version from the Toolkit)
What event can I subscribe to, to detect when a new row is added?
I'm adding this because I've just spent close to 2 hours trying to work out how to get the DataGrid to add a new row when you are bound to a collection of view models and you need to control the construction of those view models.
So the setup is that you have an ObservableCollection
that is bound to the ItemsSource
of your DataGrid. You need to create MyViewModel
yourself in the view model layer.
This is how the DataGrid appears to function when it adds a row automatically:
MyViewModel
it will bind to the row by calling the default constructor on the type. Who knows why it does this, but if MyViewModel
does not have a default constructor it will just fail to show that blank row. This is probably the place that you are stuck, because you don't have a default constructor, because you need to create the object yourself. Unfortunately you are going to need to go and add one. Again note that if the element type is an interface this is doomed to failure. The element type of the collection must be a concrete class with a default constructor.AddingNewItem
: this is where you can intercept the add operation and switch out the default constructor created instance it has created with your own instance. The AddingNewItemEventArgs.NewItem
has a setter and you can swap the current item for your own.