2011-06-06 75 views
10

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

如果我将源代码更改为BindingList,则它工作正常。然而,从我对这个主题的了解我可以理解,BindingList真的是一个WinForms的东西,并没有完全支持WPF。

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

谢谢。

+1

什么是XamDatagrid首先?你的意思是WPF工具箱datagrid?第三方组件?你能不能也请张贴一些示例代码:) – Bruno 2011-06-06 16:01:23

+1

@Bruno它是Infragistic的Datagrid版本 – Rachel 2011-06-06 16:11:31

回答

3

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接口。

0

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

0

我觉得你的运气不好。 IBindingList不会完全被网格支持,所以你会失去像排序我相信的东西。但OC不会执行AddNew行为。

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

0

如果你可以升级到2011年的NetAdvantage第2卷中添加新记录时绑定到的ObservableCollection会工作。

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

您也可以使用以前的建议从派生类派生ObservableCollection和实现IBindingList。

+0

我已升级到v11.2,但它仍然无法正常工作。当用BL替换OC时,它再次起作用。有什么设置我必须设置? – Marek 2012-11-26 09:53:00

+0

@marek无论您使用BindingList 还是ObservableCollection ,只需要在FieldLayoutSettings上设置为AllowAddNew的唯一设置。我今天早上用11.2测试了这个,AddNewRow用ObservableCollection工作。如果你可以提供一个样本,我可以看看它不工作的地方,我会看看。 – alhalama 2012-11-29 13:01:49