IListSource 介面

定義

提供物件回傳可綁定至資料來源的清單的功能。

public interface class IListSource
[System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public interface IListSource
public interface IListSource
[System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public interface IListSource
[<System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
type IListSource = interface
type IListSource = interface
[<System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
type IListSource = interface
Public Interface IListSource
衍生
屬性

範例

下列程式碼範例示範如何實作 IListSource 介面。 一個名為 EmployeeListSource for 資料綁定的元件透過 IList 實作該 GetList 方法暴露 for 資料綁定。 完整程式碼列表請參見 「如何實作 IListSource 介面」。

using System.ComponentModel;

namespace IListSourceCS;

public class EmployeeListSource : Component, IListSource
{
    public EmployeeListSource() { }

    public EmployeeListSource(IContainer container) => container.Add(this);

    #region IListSource Members

    bool IListSource.ContainsListCollection => false;

    System.Collections.IList IListSource.GetList()
    {
        BindingList<Employee> ble = DesignMode
        ? []
        : [
            new("Aaberg, Jesper", 26000000),
            new ("Aaberg, Jesper", 26000000),
            new ("Cajhen, Janko", 19600000),
            new ("Furse, Kari", 19000000),
            new ("Langhorn, Carl", 16000000),
            new ("Todorov, Teodor", 15700000),
            new ("Verebélyi, Ágnes", 15700000)
            ];

        return ble;
    }

    #endregion
}
Imports System.ComponentModel

Public Class EmployeeListSource
    Inherits Component
    Implements IListSource

    <System.Diagnostics.DebuggerNonUserCode()> _
Public Sub New(ByVal Container As System.ComponentModel.IContainer)
        MyClass.New()

        'Required for Windows.Forms Class Composition Designer support
        Container.Add(Me)

    End Sub

    <System.Diagnostics.DebuggerNonUserCode()> _
    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()

    End Sub

    'Component overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Component Designer
    'It can be modified using the Component Designer.
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

#Region "IListSource Members"

    Public ReadOnly Property ContainsListCollection() As Boolean Implements System.ComponentModel.IListSource.ContainsListCollection
        Get
            Return False
        End Get
    End Property

    Public Function GetList() As System.Collections.IList Implements System.ComponentModel.IListSource.GetList

        Dim ble As New BindingList(Of Employee)

        If Not Me.DesignMode Then
            ble.Add(New Employee("Aaberg, Jesper", 26000000))
            ble.Add(New Employee("Cajhen, Janko", 19600000))
            ble.Add(New Employee("Furse, Kari", 19000000))
            ble.Add(New Employee("Langhorn, Carl", 16000000))
            ble.Add(New Employee("Todorov, Teodor", 15700000))
            ble.Add(New Employee("Verebélyi, Ágnes", 15700000))
        End If

        Return ble

    End Function

#End Region

End Class

備註

你通常會用這個介面回傳一個可以綁定到資料來源的清單,而這個清單來自一個沒有自己實作 IList 的物件。

綁定資料可以在執行時或在設計器中進行,但每種方式都有規則。 執行時,你可以綁定以下任一種資料:

  • Array

  • 的實作者 IList,前提是該實作者具有強型別 Item[] 性質(即 並非 TypeObject)。 你可以透過將預設實作設為 Item[] 私密來達成這個目標。 如果你想建立一個遵循強型別集合規則的 , IList 你應該從 推導出來 CollectionBase

  • 實作者 ITypedList

在設計器中,你可以依照相同規則初始化綁定物件 Component

Note

IListSource 實作者可以回傳 IList 包含一組 IList 物件的 。

屬性

名稱 Description
ContainsListCollection

會取得一個值,表示該集合是否為物件集合 IList

方法

名稱 Description
GetList()

回傳 且 IList 可綁定到未實作 的 IList 物件的資料來源。

適用於

另請參閱