Search code examples
c#wpfwpfdatagrid

Add blank row at the end of a WPF datagrid conditionally (without using a default parameterless ctor)


I am trying to add a BLANK row at the end of a WPF 4.0 DataGrid conditionally (not via code-behind directly). I have tried the following but in vain -

I have tried to do get the IEditableCollectionView like this and operate upon that interface:

CollectionViewSource.GetDefaultView(myObservableCollection) as IEditableCollectionView

But, that does not work as intended because one of the factors for the CanAddNew property to be true is to add a default constructor to the underlying object of the collection (to which the Grid is bound to).

Problem: If I add a default constructor, then the grid always adds a blank row. I want to add upon a condition (checking a bool value) and not always. How do I do that without using a default parameterless constructor for my ViewModel class ?


Solution

  • I am able to do this now - I add an empty item into my Collection based on the condition. Then, if user edits the empty item of the collection, I add one more empty row programatically.