ITypedList 介面

定義

提供發現可繫結清單的結構描述 (Schema) 的功能,其中可用於繫結的屬性與要繫結物件的公用 (Public) 屬性是不同的。

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

範例

下列程式代碼範例示範如何實作 ITypedList 介面。 名為 SortableBindingList 的泛型型別衍生自 BindingList<T> 類別,並實作 ITypedList 介面。 如需完整的程式代碼清單,請參閱 如何:實作 ITypedList 介面

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Collections;
using System.Reflection;

namespace ITypedListCS
{
    [Serializable()]
    public class SortableBindingList<T> : BindingList<T>, ITypedList
    {
        [NonSerialized()]
        private 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 Attribute[] { 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)
        {   
            return 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[] 屬性 (,也就是 Type 除了) 之外 Object 的任何屬性。 您可以藉由建立私人的預設實作 Item[] 來完成這項作業。 如果您要建立 IList 遵循強型別集合規則的 ,您應該衍生自 CollectionBase

  • ITypedList實作器。

在設計工具中,您可以遵循相同的規則,將系結初始化至 Component 物件。

如需系結至數據源的詳細資訊,請參閱 System.Windows.Forms.Binding 類別。

方法

GetItemProperties(PropertyDescriptor[])

傳回代表繫結資料所用各項目屬性的 PropertyDescriptorCollection

GetListName(PropertyDescriptor[])

傳回清單的名稱。

適用於

另請參閱