简体   繁体   English

WPF ObservableCollection<t> vs 绑定列表<t></t></t>

[英]WPF ObservableCollection<T> vs BindingList<T>

In my WPF app I have a XamDataGrid.在我的 WPF 应用程序中,我有一个 XamDataGrid。 The grid is bound to an ObservableCollection.网格绑定到 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.我需要允许用户通过网格插入新行,但事实证明,为了使“添加新行”行可用,xamDataGrid 的源需要实现 IBindingList。 ObservableCollection does not implement that interface. ObservableCollection 没有实现该接口。

If I change my source to a BindingList, it works ok.如果我将源更改为 BindingList,它可以正常工作。 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.但是,根据我阅读本主题的理解,BindingList 确实是 WinForms 的东西,并且在 WPF 中不完全支持。

Would I be making a mistake if I changed all of my ObservableCollections to BindingLists?如果我将所有 ObservableCollections 都更改为 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?关于如何在将源保持为 ObservableCollection 的同时为我的 xamDataGrid 添加新的行功能,是否有人有任何其他建议? 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.据我了解,有许多不同的网格需要实现 IBindingList 以支持添加新行功能,但我看到的大多数解决方案只是切换到 BindingList。

Thanks.谢谢。

The IBindingList interface and BindingList class are defined in the System.ComponentModel namespace, and so are not strictly Windows Forms related. IBindingList接口和BindingList class 定义在 System.ComponentModel 命名空间中,因此不严格与 Windows Forms 相关。

Have you checked is xamGrid supports binding to a ICollectionView source?您是否检查过 xamGrid 是否支持绑定到ICollectionView源? If so, you could expose your data sources using this interface and back it using a BindingListCollectionView .如果是这样,您可以使用此接口公开您的数据源并使用BindingListCollectionView支持它。

You could also create a subclass of ObservableCollection<T> and implement the IBindingList interface:您还可以创建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(); }
    }
}

Alternately, you could subclass BindingList<T> and implement the INotifyCollectionChanged interface.或者,您可以继承BindingList<T>并实现INotifyCollectionChanged接口。

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.我不熟悉 IBindingList,但我可能会采用编写适配器和/或扩展 class 的方法,使 ObservableCollection 适应 IBindingList。 This way, you can keep your familiar ObservableCollection code (and also use it in other places besides the Infragistic DataGrid).这样,您可以保留您熟悉的 ObservableCollection 代码(也可以在 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.网格不会完全支持 IBindingList,所以你会失去我相信的排序之类的东西。 But OC doesn't do the AddNew behavior.但是 OC 不执行 AddNew 行为。

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.我不会使用 IBindingList,我可能只是添加一个按钮以将新项目插入列表,然后设置网格以编辑该项目。

If you can upgrade to NetAdvantage 2011 Volume 2 the add new record will work when bound to an ObservableCollection.如果您可以升级到 NetAdvantage 2011 第 2 卷,则添加新记录将在绑定到 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.如果您使用的是 NetAdvantage 2011 第 1 卷或更早版本,则 XamDataGrid 在其 CanAddNew 属性返回 true 时也支持 IEditableCollectionView 接口。 You could use a ListCollectionView giving it the instance of your ObservableCollection and then bind the XamDataGrid to the ListCollectionView.您可以使用 ListCollectionView 为其提供 ObservableCollection 的实例,然后将 XamDataGrid 绑定到 ListCollectionView。

You could also use the previous suggestion of deriving from ObservableCollection and implementing IBindingList on your derived class.您还可以使用从 ObservableCollection 派生并在派生的 class 上实现 IBindingList 的先前建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM