ITypedList Gränssnitt

Definition

Tillhandahåller funktioner för att identifiera schemat för en bindbar lista, där de egenskaper som är tillgängliga för bindning skiljer sig från de offentliga egenskaperna för objektet som ska bindas till.

public interface class ITypedList
public interface ITypedList
type ITypedList = interface
Public Interface ITypedList
Härledda

Exempel

Följande kodexempel visar hur du implementerar ITypedList-gränssnittet. En generisk typ med namnet SortableBindingList härleds från BindingList<T> klassen och implementerar ITypedList gränssnittet. En fullständig kodlista finns i Så här implementerar du ITypedList-gränssnittet.

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

Kommentarer

Använd det här gränssnittet om du till exempel använder ett DataView objekt som representerar en customer tabell. Du vill binda till egenskaperna för customer objektet som DataView representerar, inte egenskaperna för DataView.

Det här gränssnittet krävs inte för designtidsstöd för en bindbar lista.

Bindning till data kan ske antingen vid körning eller i en designer, men det finns regler för båda. Vid körning kan du binda till data i något av följande:

  • Array

  • Implementer för IList, förutsatt att implementeraren har en starkt typad Item[] egenskap (det vill: Type är allt annat än Object). Du kan göra detta genom att göra standardimplementeringen av Item[] privat. Om du vill skapa en IList som följer reglerna för en starkt typad samling bör du härleda från CollectionBase.

  • Implementerare för ITypedList.

I en designer kan du initiera bindning till objekt genom att Component följa samma regler.

Mer information om bindning till en datakälla finns i klassen System.Windows.Forms.Binding.

Metoder

Name Description
GetItemProperties(PropertyDescriptor[])

PropertyDescriptorCollection Returnerar som representerar egenskaperna för varje objekt som används för att binda data.

GetListName(PropertyDescriptor[])

Returnerar namnet på listan.

Gäller för

Se även