3
votes

Background

I'm binding a datagrid to an ObservableCollection. The ViewModels in the observable collection do not have a parameter-less constructor, so the CanUserAddNewRows evaluates to false.

From what I can tell, the ObservableCollection defaults to a ListCollectionView when bound, and which the IEditableCollectionViewAddNewItem implementation is internally checking for the parameter-less constructor; disabling the AddNew.

Question

How can I provide the new object / factory that the datagrid is requesting?

1
Can you not just add a parameter-less constructor that calls the parameterized constructor with default values? - sa_ddam213
No, a parameter-less constructor is not an option. There are dependencies that are required at the time of construction. - Michael G

1 Answers

1
votes

You can derive from ListCollectionView and customize its behavior; then you can use it as a wrapper around the ObservableCollection and bind to it instead of directly to the OC. When binding to a collection WPF creates an ICollectionView behind the scenes anyway, if you provide it directly it will use that instead.

I believe Type of object created by ListCollectionView.AddNew does something sufficiently similar?

I recommend to take some time to understand all the peculiarities of ListCollectionView though first... it's not exactly trivial, but then it's not rocket science either.