ITypedList Rozhraní

Definice

Poskytuje funkce ke zjištění schématu pro seznam s možností vazby, kde se vlastnosti dostupné pro vazbu liší od veřejných vlastností objektu, ke které se mají vytvořit vazby.

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

Příklady

Následující příklad kódu ukazuje, jak implementovat ITypedList rozhraní. Obecný typ pojmenovaný SortableBindingList je odvozen z BindingList<T> třídy a implementuje ITypedList rozhraní. Úplný výpis kódu naleznete v tématu Postupy: Implementace rozhraní 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

Poznámky

Toto rozhraní použijte, pokud například používáte DataView objekt, který představuje customer tabulku, chcete vytvořit vazbu na vlastnosti objektu customerDataView , který představuje, nikoli vlastnosti objektu DataView.

Toto rozhraní není vyžadováno pro podporu návrhu seznamu s možností vytvoření vazby.

Vazba na data může nastat buď za běhu, nebo v návrháři, ale pro obě existují pravidla. V době běhu můžete vytvořit vazbu na data v některém z následujících způsobů:

  • Array

  • Implementátor , IListza předpokladu, že implementátor má silnou Item[] typ vlastnost (to znamená, Type že je cokoli, ale Object). Toho můžete dosáhnout tak, že nastavíte výchozí implementaci privátního Item[] . Chcete-li vytvořit, IList který se řídí pravidly silného typu kolekce, měli byste odvodit z CollectionBase.

  • Implementátor nástroje ITypedList.

V návrháři můžete inicializovat vazbu na Component objekty pomocí stejných pravidel.

Další informace o vazbě ke zdroji dat najdete v System.Windows.Forms.Binding této třídě.

Metody

Name Description
GetItemProperties(PropertyDescriptor[])

PropertyDescriptorCollection Vrátí hodnotu, která představuje vlastnosti pro každou položku použitou k vytvoření vazby dat.

GetListName(PropertyDescriptor[])

Vrátí název seznamu.

Platí pro

Viz také