ITypedList Interfejs

Definicja

Udostępnia funkcje odnajdywania schematu dla listy możliwej do powiązania, gdzie właściwości dostępne dla powiązania różnią się od właściwości publicznych obiektu do powiązania.

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

Przykłady

W poniższym przykładzie kodu pokazano, jak zaimplementować ITypedList interfejs. Typ ogólny o nazwie SortableBindingList pochodzi z BindingList<T> klasy i implementuje ITypedList interfejs. Aby uzyskać pełną listę kodu, zobacz How to: Implement the ITypedList Interface (Instrukcje: implementowanie interfejsu ITypedList).

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

Uwagi

Użyj tego interfejsu, jeśli na przykład używasz obiektu reprezentującego DataView tabelę customer , chcesz powiązać z właściwościami obiektu customer reprezentowanego DataView przez obiekt, a nie właściwościami DataViewobiektu .

Ten interfejs nie jest wymagany do obsługi listy możliwej do utworzenia w czasie projektowania.

Powiązanie z danymi może wystąpić w czasie wykonywania lub w projektancie, ale istnieją reguły obu tych elementów. W czasie wykonywania można powiązać dane w dowolnym z następujących elementów:

  • Array

  • Implementer elementu IList, pod warunkiem, że implementator ma silnie typizowana Item[] właściwość (czyli jest to wszystko, Type ale Object). Można to zrobić, tworząc domyślną implementację Item[] prywatnego. Jeśli chcesz utworzyć obiekt IList , który jest zgodny z regułami silnie typizowanej kolekcji, należy pochodzić z klasy CollectionBase.

  • Implementator obiektu ITypedList.

W projektancie można zainicjować powiązanie z Component obiektami, postępując zgodnie z tymi samymi regułami.

Aby uzyskać więcej informacji na temat powiązania ze źródłem danych, zobacz klasę System.Windows.Forms.Binding .

Metody

GetItemProperties(PropertyDescriptor[])

PropertyDescriptorCollection Zwraca wartość reprezentującą właściwości każdego elementu używanego do powiązania danych.

GetListName(PropertyDescriptor[])

Zwraca nazwę listy.

Dotyczy

Zobacz też