[英]ObservableCollection bound to datagrid - Utilizing a factory method
I'm binding a datagrid to an ObservableCollection. 我正在将数据网格绑定到ObservableCollection。 The ViewModels in the observable collection do not have a parameter-less constructor, so the CanUserAddNewRows evaluates to false.
可观察集合中的ViewModel没有无参数构造函数,因此CanUserAddNewRows的计算结果为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; 据我所知,ObservableCollection在绑定时默认为ListCollectionView,IEditableCollectionViewAddNewItem实现在内部检查无参数构造函数; disabling the AddNew.
禁用AddNew。
How can I provide the new object / factory that the datagrid is requesting? 如何提供数据网格请求的新对象/工厂?
You can derive from ListCollectionView and customize its behavior; 您可以从ListCollectionView派生并自定义其行为; then you can use it as a wrapper around the ObservableCollection and bind to it instead of directly to the OC.
然后你可以将它用作ObservableCollection的包装器并绑定到它而不是直接绑定到OC。 When binding to a collection WPF creates an ICollectionView behind the scenes anyway, if you provide it directly it will use that instead.
当绑定到集合时,WPF无论如何都会在幕后创建一个ICollectionView,如果你直接提供它,它将使用它。
I believe Type of object created by ListCollectionView.AddNew does something sufficiently similar? 我相信ListCollectionView.AddNew创建的对象类型是否具有足够的相似性?
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. 我建议花一些时间来理解ListCollectionView的所有特性,虽然它首先......它并不是微不足道的,但它也不是火箭科学。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.