BindingList<T> 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供支持數據系結的泛型集合。
generic <typename T>
public ref class BindingList : System::Collections::ObjectModel::Collection<T>, System::ComponentModel::IBindingList, System::ComponentModel::ICancelAddNew, System::ComponentModel::IRaiseItemChangedEvents
[System.Serializable]
public class BindingList<T> : System.Collections.ObjectModel.Collection<T>, System.ComponentModel.IBindingList, System.ComponentModel.ICancelAddNew, System.ComponentModel.IRaiseItemChangedEvents
public class BindingList<T> : System.Collections.ObjectModel.Collection<T>, System.ComponentModel.IBindingList, System.ComponentModel.ICancelAddNew, System.ComponentModel.IRaiseItemChangedEvents
[<System.Serializable>]
type BindingList<'T> = class
inherit Collection<'T>
interface IBindingList
interface IList
interface ICollection
interface IEnumerable
interface ICancelAddNew
interface IRaiseItemChangedEvents
type BindingList<'T> = class
inherit Collection<'T>
interface ICollection
interface IEnumerable
interface IList
interface IBindingList
interface ICancelAddNew
interface IRaiseItemChangedEvents
Public Class BindingList(Of T)
Inherits Collection(Of T)
Implements IBindingList, ICancelAddNew, IRaiseItemChangedEvents
類型參數
- T
清單中的項目類型。
- 繼承
- 屬性
- 實作
範例
以下範例示範綁定到 BindingList<T> 包含業務物件的元件。 這是一個包含方法 Main 的完整範例。
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace BindingListOfTExamples;
public partial class Form1 : Form
{
readonly TextBox textBox2;
readonly ListBox listBox1;
readonly Button button1;
readonly TextBox textBox1;
readonly Random randomNumber = new();
public Form1()
{
AutoScaleMode = AutoScaleMode.Font;
textBox1 = new TextBox();
textBox2 = new TextBox();
listBox1 = new ListBox();
button1 = new Button();
textBox1.Location = new Point(169, 26);
textBox1.Size = new Size(100, 20);
textBox1.Text = "Bracket";
textBox2.Location = new Point(169, 57);
textBox2.ReadOnly = true;
textBox2.Size = new Size(100, 20);
textBox2.Text = "4343";
listBox1.FormattingEnabled = true;
listBox1.Location = new Point(12, 12);
listBox1.Size = new Size(120, 95);
button1.Location = new Point(180, 83);
button1.Size = new Size(75, 23);
button1.Text = "Add New Item";
button1.Click += button1_Click;
ClientSize = new Size(292, 266);
Controls.Add(button1);
Controls.Add(listBox1);
Controls.Add(textBox2);
Controls.Add(textBox1);
Text = "Parts Form";
Load += Form1_Load;
}
void Form1_Load(object sender, EventArgs e)
{
InitializeListOfParts();
listBox1.DataSource = listOfParts;
listBox1.DisplayMember = "PartName";
listOfParts.AddingNew += listOfParts_AddingNew;
listOfParts.ListChanged += listOfParts_ListChanged;
}
// Declare a new BindingListOfT with the Part business object.
BindingList<Part> listOfParts;
void InitializeListOfParts()
{
// Create the new BindingList of Part type.
listOfParts = new BindingList<Part>
{
// Allow new parts to be added, but not removed once committed.
AllowNew = true,
AllowRemove = false,
// Raise ListChanged events when new parts are added.
RaiseListChangedEvents = true,
// Do not allow parts to be edited.
AllowEdit = false
};
// Add a couple of parts to the list.
listOfParts.Add(new Part("Widget", 1234));
listOfParts.Add(new Part("Gadget", 5647));
}
// Create a new part from the text in the two text boxes.
void listOfParts_AddingNew(object sender, AddingNewEventArgs e) => e.NewObject = new Part(textBox1.Text, int.Parse(textBox2.Text));
// Add the new part unless the part number contains
// spaces. In that case cancel the add.
void button1_Click(object sender, EventArgs e)
{
Part newPart = listOfParts.AddNew();
if (newPart.PartName.Contains(' '))
{
_ = MessageBox.Show("Part names cannot contain spaces.");
listOfParts.CancelNew(listOfParts.IndexOf(newPart));
}
else
{
textBox2.Text = randomNumber.Next(9999).ToString();
textBox1.Text = "Enter part name";
}
}
void listOfParts_ListChanged(object sender, ListChangedEventArgs e) => MessageBox.Show(e.ListChangedType.ToString());
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
// A simple business object for example purposes.
public class Part
{
public Part() { }
public Part(string nameForPart, int numberForPart)
{
PartName = nameForPart;
PartNumber = numberForPart;
}
public string PartName { get; set; }
public int PartNumber { get; set; }
}
Option Explicit On
Option Strict On
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Class Form1
Inherits Form
Private textBox2 As TextBox
Private listBox1 As ListBox
Private WithEvents button1 As Button
Private textBox1 As TextBox
Private randomNumber As New Random()
Public Sub New()
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.textBox1 = New System.Windows.Forms.TextBox()
Me.textBox2 = New System.Windows.Forms.TextBox()
Me.listBox1 = New System.Windows.Forms.ListBox()
Me.button1 = New System.Windows.Forms.Button()
Me.textBox1.Location = New System.Drawing.Point(169, 26)
Me.textBox1.Size = New System.Drawing.Size(100, 20)
Me.textBox1.Text = "Bracket"
Me.textBox2.Location = New System.Drawing.Point(169, 57)
Me.textBox2.ReadOnly = True
Me.textBox2.Size = New System.Drawing.Size(100, 20)
Me.textBox2.Text = "4343"
Me.listBox1.FormattingEnabled = True
Me.listBox1.Location = New System.Drawing.Point(12, 12)
Me.listBox1.Size = New System.Drawing.Size(120, 95)
Me.button1.Location = New System.Drawing.Point(180, 83)
Me.button1.Size = New System.Drawing.Size(75, 23)
Me.button1.Text = "Add New Item"
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.button1)
Me.Controls.Add(Me.listBox1)
Me.Controls.Add(Me.textBox2)
Me.Controls.Add(Me.textBox1)
Me.Text = "Parts Form"
AddHandler Me.Load, AddressOf Form1_Load
End Sub
Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
InitializeListOfParts()
listBox1.DataSource = listOfParts
listBox1.DisplayMember = "PartName"
End Sub
' Declare a new BindingListOfT with the Part business object.
Private WithEvents listOfParts As BindingList(Of Part)
Private Sub InitializeListOfParts()
' Create the new BindingList of Part type.
listOfParts = New BindingList(Of Part)
' Allow new parts to be added, but not removed once committed.
listOfParts.AllowNew = True
listOfParts.AllowRemove = False
' Raise ListChanged events when new parts are added.
listOfParts.RaiseListChangedEvents = True
' Do not allow parts to be edited.
listOfParts.AllowEdit = False
' Add a couple of parts to the list.
listOfParts.Add(New Part("Widget", 1234))
listOfParts.Add(New Part("Gadget", 5647))
End Sub
' Create a new part from the text in the two text boxes.
Private Sub listOfParts_AddingNew(ByVal sender As Object, _
ByVal e As AddingNewEventArgs) Handles listOfParts.AddingNew
e.NewObject = New Part(textBox1.Text, Integer.Parse(textBox2.Text))
End Sub
' Add the new part unless the part number contains
' spaces. In that case cancel the add.
Private Sub button1_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles button1.Click
Dim newPart As Part = listOfParts.AddNew()
If newPart.PartName.Contains(" ") Then
MessageBox.Show("Part names cannot contain spaces.")
listOfParts.CancelNew(listOfParts.IndexOf(newPart))
Else
textBox2.Text = randomNumber.Next(9999).ToString()
textBox1.Text = "Enter part name"
End If
End Sub
<STAThread()> _
Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(New Form1())
End Sub
End Class
' A simple business object for example purposes.
Public Class Part
Private name As String
Private number As Integer
Public Sub New()
End Sub
Public Sub New(ByVal nameForPart As String, _
ByVal numberForPart As Integer)
PartName = nameForPart
PartNumber = numberForPart
End Sub
Public Property PartName() As String
Get
Return name
End Get
Set(ByVal value As String)
name = Value
End Set
End Property
Public Property PartNumber() As Integer
Get
Return number
End Get
Set(ByVal value As Integer)
number = Value
End Set
End Property
End Class
備註
此 BindingList<T> 類別可作為基底類別,建立雙向資料綁定機制。 BindingList<T> 提供一個具體且通用的 IBindingList 介面實作。 這是實現完整IBindingList介面的替代方案,因為完整介面因 、 IEditableObject與 CurrencyManager之間的微妙互動IBindingList而較為困難。 然而,典型的解決方案程式設計師會使用提供資料綁定功能的類別,例如 BindingSource,而非直接使用 BindingList<T>。
BindingList<T> 透過可 AddNew 擴充方法支援工廠建立的實例。 (類似的可擴展性也存在於其他類別中,例如 BindingSource)此外,由於此類別實作了介面,ICancelAddNew它能透過 and CancelNew 方法對新項目EndNew進行交易提交或回滾。
建構函式
| 名稱 | Description |
|---|---|
| BindingList<T>() |
使用預設值初始化該類別的新實例 BindingList<T> 。 |
| BindingList<T>(IList<T>) |
初始化一個新的類別實例 BindingList<T> ,使用指定的清單。 |
屬性
| 名稱 | Description |
|---|---|
| AllowEdit |
取得或設定一個值,指示列表中的項目是否可以被編輯。 |
| AllowNew |
會取得或設定一個值,指示你是否可以用該 AddNew() 方法將項目加入清單。 |
| AllowRemove |
會取得或設定一個值,表示你是否可以從收藏中移除項目。 |
| Count |
取得實際包含於 Collection<T>的元素數量。 (繼承來源 Collection<T>) |
| IsSortedCore |
會獲得一個值,表示清單是否已排序。 |
| Item[Int32] |
取得或設定位於指定索引處的專案。 (繼承來源 Collection<T>) |
| Items |
會被 IList<T> 包裹在 Collection<T>. (繼承來源 Collection<T>) |
| RaiseListChangedEvents |
取得或設定一個值,指示列表中新增或移除項目是否會 ListChanged 引發事件。 |
| SortDirectionCore |
它會得到清單排序的方向。 |
| SortPropertyCore |
若排序實作於衍生類別,則取得用於排序清單的屬性描述符;否則,返回 |
| SupportsChangeNotificationCore |
會獲得一個值,表示事件是否 ListChanged 已啟用。 |
| SupportsSearchingCore |
會獲得一個值,表示該清單是否支援搜尋。 |
| SupportsSortingCore |
會獲得一個值,表示該清單是否支援排序。 |
方法
事件
| 名稱 | Description |
|---|---|
| AddingNew |
發生在項目加入清單之前。 |
| ListChanged |
當清單或清單中的項目改變時,會發生這種情況。 |