繁体   English   中英

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

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

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

如果我将源更改为 BindingList,它可以正常工作。 但是,根据我阅读本主题的理解,BindingList 确实是 WinForms 的东西,并且在 WPF 中不完全支持。

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

谢谢。

IBindingList接口和BindingList class 定义在 System.ComponentModel 命名空间中,因此不严格与 Windows Forms 相关。

您是否检查过 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接口。

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

我认为无论哪种方式你都不走运。 网格不会完全支持 IBindingList,所以你会失去我相信的排序之类的东西。 但是 OC 不执行 AddNew 行为。

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

如果您可以升级到 NetAdvantage 2011 第 2 卷,则添加新记录将在绑定到 ObservableCollection 时起作用。

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

您还可以使用从 ObservableCollection 派生并在派生的 class 上实现 IBindingList 的先前建议。

暂无
暂无

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

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