簡體   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