c# – WPF ObservableCollection对BindingList
发布时间:2020-12-15 06:33:54 所属栏目:百科 来源:网络整理
导读:在我的 WPF应用程序中,我有一个XamDataGrid.网格绑定到一个ObservableCollection.我需要允许用户通过网格插入新行,但事实证明,为了“添加新行”行可用,xamDataGrid的源需要实现IBindingList. ObservableCollection不实现该接口. 如果我将源更改为BindingList
在我的
WPF应用程序中,我有一个XamDataGrid.网格绑定到一个ObservableCollection.我需要允许用户通过网格插入新行,但事实证明,为了“添加新行”行可用,xamDataGrid的源需要实现IBindingList. ObservableCollection不实现该接口.
如果我将源更改为BindingList,它可以正常工作.然而,从阅读本主题可以理解的是,BindingList实际上是一个WinForms的东西,在WPF中并不完全支持. 如果我将所有可观察的选项更改为BindingLists,我会犯错误?有没有人有任何其他建议,如何可以为我的xamDataGrid添加新行功能,同时保持源作为ObservableCollection?我的理解是,有许多不同的网格需要实现IBindingList以支持添加新的行功能,但是我看到的大多数解决方案只是切换到BindingList. 谢谢. 解决方法
IBindingList界面和
BindingList类在System.ComponentModel命名空间中定义,所以不是严格的Windows窗体相关的.
你检查过xamGrid是否支持绑定到ICollectionView源?如果是这样,您可以使用此界面公开数据源,并使用BindingListCollectionView备份. 您也可以创建一个ObservableCollection< T>的子类.并实现IBindingList接口: using System; using System.ComponentModel; using System.Collections.Generic; using System.Collections.ObjectModel; public class ObservableBindingList<T> : ObservableCollection<T>,IBindingList { // Constructors public ObservableBindingList() : base() { } public ObservableBindingList(IEnumerable<T> collection) : base(collection) { } public ObservableBindingList(List<T> 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(); } } } 或者,您可以将BindingList< T>并实现INotifyCollectionChanged接口. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |