ITypedList 接口

定义

提供发现可绑定列表架构的功能,其中可用于绑定的属性不同于要绑定到的对象的公共属性。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. 名为的泛型类型 SortableBindingListBindingList<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
{
    [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 ,则需要绑定到表示的对象的属性 customer DataView ,而不是绑定的属性 DataViewUse 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[] 属性 (也就是说,它 Type 是但 Object) 的。Implementer of IList, provided the implementer has a strongly typed Item[] property (that is, the Type is anything but Object). 您可以通过将默认实现设为私有来实现此目的 Item[]You can accomplish this by making the default implementation of Item[] private. 如果要创建 IList 遵循强类型集合的规则的,则应派生自 CollectionBaseIf you want to create an IList that follows the rules of a strongly typed collection, you should derive from CollectionBase.

  • 的实施者 ITypedListImplementer 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.

方法

GetItemProperties(PropertyDescriptor[])

返回 PropertyDescriptorCollection,其表示每一项上用于绑定数据的属性。Returns the PropertyDescriptorCollection that represents the properties on each item used to bind data.

GetListName(PropertyDescriptor[])

返回列表名称。Returns the name of the list.

适用于

另请参阅