简体   繁体   English

绑定到datagrid的ObservableCollection - 使用工厂方法

[英]ObservableCollection bound to datagrid - Utilizing a factory method

Background 背景

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。

Question

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.

相关问题 DataGrid未绑定到ObservableCollection - DataGrid is not bound to the ObservableCollection 绑定了ObservableCollection的dataGrid中的计算列 - Calculated column in a dataGrid bound with a ObservableCollection 在 MVVM 中对绑定到 DataGrid 的 ObservableCollection 进行排序 - Sort ObservableCollection bound to DataGrid in MVVM 访问绑定到 WPF DataGrid 的 ObservableCollection 中的项目 - Accessing items in ObservableCollection bound to WPF DataGrid 访问ObservableCollection中的项目绑定到WPF DataGrid - Accesing items in ObservableCollection bound to WPF DataGrid WPF将项添加到绑定到observablecollection异常的datagrid - WPF add item to datagrid bound to observablecollection exception 从绑定到数据网格Silverlight的observablecollection中删除项目? - Removing items from a observablecollection bound to a datagrid Silverlight? 删除绑定到 ObservableCollection 的 DataGrid 中的选定行 - Delete selected row in DataGrid bound to an ObservableCollection 在 ViewModel 中修改 ObservableCollection 时绑定到 ObservableCollection 的 DataGrid MultiSelect 不稳定 - DataGrid MultiSelect bound to ObservableCollection unstable when ObservableCollection is modified in the ViewModel 问:C#WPF数据网格绑定到ObservableCollection <T> 没有更新 - Q: C# WPF DataGrid bound to ObservableCollection<T> gets not updated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM