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

注解

例如,如果使用表示DataViewcustomer表的对象,则使用此接口,而希望绑定到所表示对象的属性customer,而不是该对象的DataView属性DataView

对于可绑定列表的设计时支持,不需要此接口。

绑定到数据可以在运行时或在设计器中发生,但两者都有规则。 在运行时,可以绑定到以下任一项中的数据:

  • Array

  • 实现者,前提是实现者 IList具有强类型 Item[] 属性(即, Type 除了任何属性 Object)。 可以通过创建专用的默认实现 Item[] 来实现此目的。 如果要创建一个遵循强类型集合规则的集合 IList ,则应派生自 CollectionBase

  • ITypedList实现者

在设计器中,可以遵循相同的规则来初始化绑定到 Component 对象的绑定。

有关绑定到数据源的详细信息,请参阅该 System.Windows.Forms.Binding 类。

方法

名称 说明
GetItemProperties(PropertyDescriptor[])

返回表示 PropertyDescriptorCollection 用于绑定数据的每个项的属性。

GetListName(PropertyDescriptor[])

返回列表的名称。

适用于

另请参阅