ITypedList 介面

定義

提供發現可綁定清單結構的功能,該清單中可綁定的屬性與物件的公開屬性不同。

public interface class ITypedList
public interface ITypedList
type ITypedList = interface
Public Interface ITypedList
衍生

範例

下列程式碼範例示範如何實作 ITypedList 介面。 一個名為 SortableBindingList 的通用型別源自類別 BindingList<T> 並實作介面 ITypedList 。 完整程式碼列表請參見 「如何實作 ITypedList 介面」。

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace ITypedListCS;

[Serializable()]
public class SortableBindingList<T> : BindingList<T>, ITypedList
{
    [NonSerialized()]
    readonly PropertyDescriptorCollection properties;

    public SortableBindingList() : base()
    {
        // Get the 'shape' of the list. 
        // Only get the public properties marked with Browsable = true.
        PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(
            typeof(T),
            [new BrowsableAttribute(true)]);

        // Sort the properties.
        properties = pdc.Sort();
    }

    #region ITypedList Implementation

    public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
    {
        PropertyDescriptorCollection pdc;

        if (listAccessors != null && listAccessors.Length > 0)
        {
            // Return child list shape.
            pdc = ListBindingHelper.GetListItemProperties(listAccessors[0].PropertyType);
        }
        else
        {
            // Return properties in sort order.
            pdc = properties;
        }

        return pdc;
    }

    // This method is only used in the design-time framework 
    // and by the obsolete DataGrid control.
    public string GetListName(PropertyDescriptor[] listAccessors) => typeof(T).Name;

    #endregion
}
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Windows.Forms

<Serializable()> _
Public Class SortableBindingList(Of Tkey)
    Inherits BindingList(Of Tkey)
    Implements ITypedList

    <NonSerialized()> _
    Private properties As PropertyDescriptorCollection

    Public Sub New()
        MyBase.New()

        ' Get the 'shape' of the list. 
        ' Only get the public properties marked with Browsable = true.
        Dim pdc As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(Tkey), New Attribute() {New BrowsableAttribute(True)})

        ' Sort the properties.
        properties = pdc.Sort()

    End Sub

#Region "ITypedList Implementation"

    Public Function GetItemProperties(ByVal listAccessors() As System.ComponentModel.PropertyDescriptor) As System.ComponentModel.PropertyDescriptorCollection Implements System.ComponentModel.ITypedList.GetItemProperties

        Dim pdc As PropertyDescriptorCollection

        If (Not (listAccessors Is Nothing)) And (listAccessors.Length > 0) Then
            ' Return child list shape
            pdc = ListBindingHelper.GetListItemProperties(listAccessors(0).PropertyType)
        Else
            ' Return properties in sort order
            pdc = properties
        End If

        Return pdc

    End Function

    ' This method is only used in the design-time framework 
    ' and by the obsolete DataGrid control.
    Public Function GetListName( _
    ByVal listAccessors() As PropertyDescriptor) As String _
    Implements System.ComponentModel.ITypedList.GetListName

        Return GetType(Tkey).Name

    End Function

#End Region

End Class

備註

例如,如果你使用DataView代表一個customer資料表的物件,你想綁定到該DataView物件上的customer屬性,而不是 的屬性DataView,可以使用這個介面。

此介面並非設計時支援可綁定清單所必需的。

綁定資料可以在執行時或在設計器中進行,但兩者都有規則。 執行時,你可以綁定以下任一種資料:

  • Array

  • 的實作者 IList,前提是該實作者具有強型別 Item[] 性質(即 並非 TypeObject)。 你可以透過將預設實作設為 Item[] 私密來達成這個目標。 如果你想建立一個遵循強型別集合規則的 , IList 你應該從 推導出來 CollectionBase

  • 實作者 ITypedList

在設計器中,你可以依照相同規則初始化綁定物件 Component

欲了解更多綁定資料來源的資訊,請參閱該 System.Windows.Forms.Binding 類別。

方法

名稱 Description
GetItemProperties(PropertyDescriptor[])

回傳 PropertyDescriptorCollection 代表每個用於綁定資料的項目屬性。

GetListName(PropertyDescriptor[])

回傳清單名稱。

適用於

另請參閱