Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ObservableCollection<T> vs BindingList<T>

In my WPF app I have a XamDataGrid. The grid is bound to an ObservableCollection. I need to allow users to insert new rows through the grid but it turns out that in order for the "Add New Row" row to be available, the xamDataGrid's source needs to implement IBindingList. ObservableCollection does not implement that interface.

If I change my source to a BindingList, it works ok. However, from what I can understand from reading up on this topic, BindingList is really a WinForms thing and is not fully supported in WPF.

Would I be making a mistake if I changed all of my ObservableCollections to BindingLists? Does anyone have any other suggestions as to how I can get add new row functionality for my xamDataGrid while keeping the source as an ObservableCollection? It is my understanding that there are a number of different grids that require IBindingList to be implemented in order to support add new row functionality but most solutions I see are to just switch to BindingList.

Thanks.

like image 796
Flack Avatar asked Jun 06 '11 15:06

Flack


People also ask

What is the difference between ObservableCollection and BindingList?

The practical difference is that BindingList is for WinForms, and ObservableCollection is for WPF. From a WPF perspective, BindingList isnt properly supported, and you would never really use it in a WPF project unless you really had to.

What is ObservableCollection in WPF?

WPF ObservableCollection An ObservableCollection is a dynamic collection of objects of a given type. Objects can be added, removed or be updated with an automatic notification of actions. When an object is added to or removed from an observable collection, the UI is automatically updated.

What is binding list in C#?

The BindingList<T> class can be used as a base class to create a two-way data-binding mechanism.


1 Answers

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

Have you checked if 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.

like image 132
Oppositional Avatar answered Sep 19 '22 23:09

Oppositional