简体   繁体   中英

ObservableCollection bound to datagrid - Utilizing a factory method

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM