ITypedList 介面


提供發現可繫結清單的結構描述 (Schema) 的功能,其中可用於繫結的屬性與要繫結物件的公用 (Public) 屬性是不同的。Provides functionality to discover the schema for a bindable list, where the properties available for binding differ from the public properties of the object to bind to.

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


下列程式碼範例示範如何執行ITypedList介面。The following code example demonstrates how to implement the ITypedList interface. 名為SortableBindingList的泛型型別衍生BindingList<T>自類別, 並ITypedList實作為介面。A generic type named SortableBindingList derives from the BindingList<T> class and implements the ITypedList interface. 如需完整的程式代碼清單, 請參閱如何:執行 ITypedList 介面For a full code listing, see How to: Implement the ITypedList Interface.

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

namespace ITypedListCS
    public class SortableBindingList<T> : BindingList<T>, ITypedList
        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(
                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);
                // 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;

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()

        ' 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)
            ' 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代表資料表的物件, 而您想要系結customerDataViewDataView代表物件的屬性, 而不是的屬性, 請使用此介面。Use this interface if, for instance, you are using a DataView object that represents a customer table, you want to bind to the properties on the customer object that the DataView represents, not the properties of the DataView.

可系結清單的設計階段支援不需要此介面。This interface is not required for design-time support of a bindable list.

系結至資料可能會在執行時間或設計工具中發生, 但兩者都有規則。Binding to data can occur either at run time or in a designer, but there are rules for both. 在執行時間, 您可以系結至下列任一項的資料:At run time, you can bind to data in any of the following:

  • Array

  • IList實施者, 前提是實施者具有Item[Int32]強型別屬性Type (亦即, 是任何Object專案, 但不包括)。Implementer of IList, provided the implementer has a strongly typed Item[Int32] property (that is, the Type is anything but Object). 您可以藉由進行私用的預設實Item[Int32]值來完成這項工作。You can accomplish this by making the default implementation of Item[Int32] private. 如果您想要建立IList遵循強型別集合規則的, 您應該衍生自。 CollectionBaseIf you want to create an IList that follows the rules of a strongly typed collection, you should derive from CollectionBase.

  • ITypedList實施者。Implementer of ITypedList.

在設計工具中, 您可以遵循相同Component的規則, 初始化物件的系結。In a designer, you can initialize binding to Component objects by following the same rules.

如需系結至資料來源的詳細資訊, 請System.Windows.Forms.Binding參閱類別。For more information on binding to a data source, see the System.Windows.Forms.Binding class.



傳回代表繫結資料所用各項目屬性的 PropertyDescriptorCollectionReturns the PropertyDescriptorCollection that represents the properties on each item used to bind data.


傳回清單的名稱。Returns the name of the list.