WPF ObservableCollection vs BindingList

在我的WPF应用程序中,我有一个XamDataGrid。 网格绑定到ObservableCollection。 我需要允许用户通过网格插入新行,但事实certificate,为了使“添加新行”行可用,xamDataGrid的源需要实现IBindingList。 ObservableCollection不实现该接口。

如果我将我的源更改为BindingList,它可以正常工作。 但是,从阅读这个主题我可以理解,BindingList实际上是一个WinForms的东西,在WPF中并不完全支持。

如果我将所有ObservableCollections更改为BindingLists,我会犯错吗? 有没有人有任何其他的建议,我如何为我的xamDataGrid添加新的行function,同时将源保持为ObservableCollection? 我的理解是,有许多不同的网格需要实现IBindingList才能支持添加新的行function,但我看到的大多数解决方案只是切换到BindingList。

谢谢。

IBindingList接口和BindingList类在System.ComponentModel命名空间中定义,因此不是严格的Windows窗体相关。

您检查过xamGrid是否支持绑定到ICollectionView源? 如果是这样,您可以使用此接口公开数据源并使用BindingListCollectionView将其备份。

您还可以创建ObservableCollection的子类并实现IBindingList接口:

 using System; using System.ComponentModel; using System.Collections.Generic; using System.Collections.ObjectModel; public class ObservableBindingList : ObservableCollection, IBindingList { // Constructors public ObservableBindingList() : base() { } public ObservableBindingList(IEnumerable collection) : base(collection) { } public ObservableBindingList(List list) : base(list) { } // IBindingList Implementation public void AddIndex(PropertyDescriptor property) { throw new NotImplementedException(); } public object AddNew() { throw new NotImplementedException(); } public bool AllowEdit { get { throw new NotImplementedException(); } } public bool AllowNew { get { throw new NotImplementedException(); } } public bool AllowRemove { get { throw new NotImplementedException(); } } public void ApplySort(PropertyDescriptor property, ListSortDirection direction) { throw new NotImplementedException(); } public int Find(PropertyDescriptor property, object key) { throw new NotImplementedException(); } public bool IsSorted { get { throw new NotImplementedException(); } } public event ListChangedEventHandler ListChanged; public void RemoveIndex(PropertyDescriptor property) { throw new NotImplementedException(); } public void RemoveSort() { throw new NotImplementedException(); } public ListSortDirection SortDirection { get { throw new NotImplementedException(); } } public PropertyDescriptor SortProperty { get { throw new NotImplementedException(); } } public bool SupportsChangeNotification { get { throw new NotImplementedException(); } } public bool SupportsSearching { get { throw new NotImplementedException(); } } public bool SupportsSorting { get { throw new NotImplementedException(); } } } 

或者,您可以inheritance BindingList并实现INotifyCollectionChanged接口。

我不熟悉IBindingList,但我可能会采用编写适配器和/或扩展类的方法来使ObservableCollection适应IBindingList。 这样,您可以保留熟悉的ObservableCollection代码(并且还可以在Infragistic DataGrid之外的其他位置使用它)。

无论如何,我认为你运气不好。 网格不会完全支持IBindingList,因此您将丢失排序我相信的东西。 但OC不执行AddNew行为。

我不会使用IBindingList,我可能只是添加一个按钮来将新项目插入到列表中,然后设置网格来编辑该项目。

这些链接会有帮助吗?

http://xceed.com/CS/blogs/dontpanic/archive/2009/04/01/i-notify-we-notify-we-all-wait-no-we-don-t.aspx (非常有用,即使标记为“Xceed”;)

ObservableCollection(Of T)vs BindingList(Of T)?

如果您可以升级到NetAdvantage 2011第2卷,则在绑定到ObservableCollection时,添加新记录将起作用。

如果您使用的是NetAdvantage 2011第1卷或更早版本,那么当其CanAddNew属性返回true时,XamDataGrid也支持IEditableCollectionView接口。 您可以使用ListCollectionView为其提供ObservableCollection的实例,然后将XamDataGrid绑定到ListCollectionView。

您还可以使用先前从ObservableCollection派生并在派生类上实现IBindingList的建议。