IBindingList 介面

定義

提供繫結至資料來源時支援複雜和簡單案例所需的功能。

C#
public interface IBindingList : System.Collections.IList
衍生
實作

範例

下列範例提供介面的 IBindingList 簡單實作。 類別會將 CustomerList 客戶資訊儲存在清單中。 此範例假設您已使用 Customer 類別中可在 類別的範例中找到的 IEditableObject 類別。

C#
public class CustomersList :  CollectionBase, IBindingList
{

    private ListChangedEventArgs resetEvent = new ListChangedEventArgs(ListChangedType.Reset, -1);
    private ListChangedEventHandler onListChanged;

    public void LoadCustomers()
    {
        IList l = (IList)this;
        l.Add(ReadCustomer1());
        l.Add(ReadCustomer2());
        OnListChanged(resetEvent);
    }

    public Customer this[int index]
    {
        get
        {
            return (Customer)(List[index]);
        }
        set
        {
            List[index] = value;
        }
    }

    public int Add (Customer value)
    {
        return List.Add(value);
    }

    public Customer AddNew()
    {
        return (Customer)((IBindingList)this).AddNew();
    }

    public void Remove (Customer value)
    {
        List.Remove(value);
    }

    protected virtual void OnListChanged(ListChangedEventArgs ev)
    {
        if (onListChanged != null)
        {
            onListChanged(this, ev);
        }
    }

    protected override void OnClear()
    {
        foreach (Customer c in List)
        {
            c.Parent = null;
        }
    }

    protected override void OnClearComplete()
    {
        OnListChanged(resetEvent);
    }

    protected override void OnInsertComplete(int index, object value)
    {
        Customer c = (Customer)value;
        c.Parent = this;
        OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, index));
    }

    protected override void OnRemoveComplete(int index, object value)
    {
        Customer c = (Customer)value;
        c.Parent = this;
        OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, index));
    }

    protected override void OnSetComplete(int index, object oldValue, object newValue)
    {
        if (oldValue != newValue)
        {

            Customer oldcust = (Customer)oldValue;
            Customer newcust = (Customer)newValue;

            oldcust.Parent = null;
            newcust.Parent = this;

            OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, index));
        }
    }

    // Called by Customer when it changes.
    internal void CustomerChanged(Customer cust)
    {
        
        int index = List.IndexOf(cust);

        OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, index));
    }

    // Implements IBindingList.
    bool IBindingList.AllowEdit
    {
        get { return true ; }
    }

    bool IBindingList.AllowNew
    {
        get { return true ; }
    }

    bool IBindingList.AllowRemove
    {
        get { return true ; }
    }

    bool IBindingList.SupportsChangeNotification
    {
        get { return true ; }
    }

    bool IBindingList.SupportsSearching
    {
        get { return false ; }
    }

    bool IBindingList.SupportsSorting
    {
        get { return false ; }
    }

    // Events.
    public event ListChangedEventHandler ListChanged
    {
        add
        {
            onListChanged += value;
        }
        remove
        {
            onListChanged -= value;
        }
    }

    // Methods.
    object IBindingList.AddNew()
    {
        Customer c = new Customer(this.Count.ToString());
        List.Add(c);
        return c;
    }

    // Unsupported properties.
    bool IBindingList.IsSorted
    {
        get { throw new NotSupportedException(); }
    }

    ListSortDirection IBindingList.SortDirection
    {
        get { throw new NotSupportedException(); }
    }

    PropertyDescriptor IBindingList.SortProperty
    {
        get { throw new NotSupportedException(); }
    }

    // Unsupported Methods.
    void IBindingList.AddIndex(PropertyDescriptor property)
    {
        throw new NotSupportedException();
    }

    void IBindingList.ApplySort(PropertyDescriptor property, ListSortDirection direction)
    {
        throw new NotSupportedException();
    }

    int IBindingList.Find(PropertyDescriptor property, object key)
    {
        throw new NotSupportedException();
    }

    void IBindingList.RemoveIndex(PropertyDescriptor property)
    {
        throw new NotSupportedException();
    }

    void IBindingList.RemoveSort()
    {
        throw new NotSupportedException();
    }

    // Worker functions to populate the list with data.
    private static Customer ReadCustomer1()
    {
        Customer cust = new Customer("536-45-1245");
        cust.FirstName = "Jo";
        cust.LastName = "Brown";
        return cust;
    }

    private static Customer ReadCustomer2()
    {
        Customer cust = new Customer("246-12-5645");
        cust.FirstName = "Robert";
        cust.LastName = "Brown";
        return cust;
    }
}

備註

這個介面是由類別實作 DataView 。 方法的實作應該與 類別中 DataView 該方法的實作相同行為。

當您呼叫 ApplySortRemoveSort 方法時,應該使用 Reset 列舉引發ListChanged事件。

當您呼叫 AddNew 方法時,應該使用具有適當索引的ItemAdded列舉引發 ListChanged 事件。 新增的數據列處於按下控制件上的 DataGridView ESC 可移除新數據列的狀態。 ListChanged在這個數據列上,使用列舉引發事件ItemAdded,表示專案現在是不是處於「新」狀態的數據列。

當您移除專案或在新的數據列上呼叫 CancelEdit 方法 (時,如果該數據列實 IEditableObject 作) ,則應該引發 ListChanged 具有適當索引之 ItemDeleted 列舉的事件。

屬性

AllowEdit

取得值,指出您是否可以更新清單中的項目。

AllowNew

取得值,指出您是否可以使用 AddNew() 將項目加入清單。

AllowRemove

取得值,指出您是否可以使用 Remove(Object)RemoveAt(Int32) 從清單移除項目。

Count

取得 ICollection 中所包含的項目數。

(繼承來源 ICollection)
IsFixedSize

取得值,指出 IList 是否有固定的大小。

(繼承來源 IList)
IsReadOnly

取得值,指出 IList 是否唯讀。

(繼承來源 IList)
IsSorted

取得值,指出清單中項目是否已排序。

IsSynchronized

取得值,這個值表示對 ICollection 的存取是否同步 (安全執行緒)。

(繼承來源 ICollection)
Item[Int32]

在指定的索引位置上取得或設定項目。

(繼承來源 IList)
SortDirection

取得排序的方向。

SortProperty

取得已經用來排序的 PropertyDescriptor

SupportsChangeNotification

取得值,指出當清單變更或清單項目變更時是否引發 ListChanged 事件。

SupportsSearching

取得值,指出清單是否支援使用 Find(PropertyDescriptor, Object) 方法進行搜尋。

SupportsSorting

取得值,指出清單是否支援排序。

SyncRoot

取得可用以同步存取 ICollection 的物件。

(繼承來源 ICollection)

方法

Add(Object)

將項目加入至 IList

(繼承來源 IList)
AddIndex(PropertyDescriptor)

PropertyDescriptor 加入用來搜尋的索引中。

AddNew()

將新的項目加入至清單中。

ApplySort(PropertyDescriptor, ListSortDirection)

根據 PropertyDescriptorListSortDirection 來排序清單。

Clear()

IList 中移除所有項目。

(繼承來源 IList)
Contains(Object)

判斷 IList 是否包含特定值。

(繼承來源 IList)
CopyTo(Array, Int32)

從特定的 ICollection 索引開始,將 Array 的項目複製到 Array

(繼承來源 ICollection)
Find(PropertyDescriptor, Object)

傳回具有指定 PropertyDescriptor 的列索引。

GetEnumerator()

傳回逐一查看集合的列舉值。

(繼承來源 IEnumerable)
IndexOf(Object)

判斷 IList 中指定項目的索引。

(繼承來源 IList)
Insert(Int32, Object)

將項目插入位於指定索引的 IList

(繼承來源 IList)
Remove(Object)

IList 移除特定物件之第一個符合的元素。

(繼承來源 IList)
RemoveAt(Int32)

移除在指定索引處的 IList 項目。

(繼承來源 IList)
RemoveIndex(PropertyDescriptor)

從用來搜尋的索引中移除 PropertyDescriptor

RemoveSort()

移除任何使用 ApplySort(PropertyDescriptor, ListSortDirection) 的套用排序。

事件

ListChanged

發生於清單變更或清單項目變更時。

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1