WPF ObservableCollection<T> vs BindingList<T>

筅森魡賤 提交于 2019-11-30 12:02:17

The IBindingList interface and BindingList class are defined in the System.ComponentModel namespace, and so are not strictly Windows Forms related.

Have you checked is xamGrid supports binding to a ICollectionView source? If so, you could expose your data sources using this interface and back it using a BindingListCollectionView.

You could also create a subclass of ObservableCollection<T> and implement the IBindingList interface:

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(); }
    }
}

Alternately, you could subclass BindingList<T> and implement the INotifyCollectionChanged interface.

I am not familiar with IBindingList, but I would probably take the approach of writing an adapter and/or extension class that adapts an ObservableCollection to an IBindingList. This way, you can keep your familiar ObservableCollection code (and also use it in other places besides the Infragistic DataGrid).

I think you're out of luck either way. IBindingList won't fully be supported by the grid, so you'll lose things like sorting I beleive. But OC doesn't do the AddNew behavior.

I wouldn't use IBindingList, I'd probably just add a button to insert a new item into the list and then set the grid to edit that item.

If you can upgrade to NetAdvantage 2011 Volume 2 the add new record will work when bound to an ObservableCollection.

If you are using NetAdvantage 2011 Volume 1 or older, then the XamDataGrid also supports the IEditableCollectionView interface when its CanAddNew property returns true. You could use a ListCollectionView giving it the instance of your ObservableCollection and then bind the XamDataGrid to the ListCollectionView.

You could also use the previous suggestion of deriving from ObservableCollection and implementing IBindingList on your derived class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!