BindingList<T> 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供支持數據系結的泛型集合。
generic <typename T>
public ref class BindingList : 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]
public class BindingList<T> : System.Collections.ObjectModel.Collection<T>, System.ComponentModel.IBindingList, System.ComponentModel.ICancelAddNew, System.ComponentModel.IRaiseItemChangedEvents
type BindingList<'T> = class
inherit Collection<'T>
interface ICollection
interface IEnumerable
interface IList
interface IBindingList
interface ICancelAddNew
interface IRaiseItemChangedEvents
[<System.Serializable>]
type BindingList<'T> = class
inherit Collection<'T>
interface IBindingList
interface IList
interface ICollection
interface IEnumerable
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.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace BindingListOfTExamples
{
public partial class Form1 : Form
{
private TextBox textBox2;
private ListBox listBox1;
private Button button1;
private TextBox textBox1;
Random randomNumber = new Random();
public Form1()
{
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.textBox1.Location = new System.Drawing.Point(169, 26);
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.Text = "Bracket";
this.textBox2.Location = new System.Drawing.Point(169, 57);
this.textBox2.ReadOnly = true;
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.Text = "4343";
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(12, 12);
this.listBox1.Size = new System.Drawing.Size(120, 95);
this.button1.Location = new System.Drawing.Point(180, 83);
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.Text = "Add New Item";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Text = "Parts Form";
this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
InitializeListOfParts();
listBox1.DataSource = listOfParts;
listBox1.DisplayMember = "PartName";
listOfParts.AddingNew += new AddingNewEventHandler(listOfParts_AddingNew);
listOfParts.ListChanged += new ListChangedEventHandler(listOfParts_ListChanged);
}
// Declare a new BindingListOfT with the Part business object.
BindingList<Part> listOfParts;
private void InitializeListOfParts()
{
// Create the new BindingList of Part type.
listOfParts = new BindingList<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));
}
// 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.
private 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
{
private string name;
private int number;
public Part() { }
public Part(string nameForPart, int numberForPart)
{
PartName = nameForPart;
PartNumber = numberForPart;
}
public string PartName
{
get { return name; }
set { name = value; }
}
public int PartNumber
{
get { return number; }
set { number = value; }
}
}
}
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 介面的替代方法,因為 IBindingList、IEditableObject和相關聯 CurrencyManager之間的細微互動,這很困難。 不過,一般解決方案程式設計人員會使用提供資料系結功能的類別,例如 BindingSource,而不是直接使用 BindingList<T>。
BindingList<T> 透過可延伸 AddNew 方法支援原廠建立的實例。 (其他類別也會找到這種相同的擴充性,例如 BindingSource)此外,由於這個類別會實作 ICancelAddNew 介面,因此會透過 EndNew 和 CancelNew 方法啟用新專案的交易式認可或復原。
建構函式
BindingList<T>() |
使用預設值,初始化 BindingList<T> 類別的新實例。 |
BindingList<T>(IList<T>) |
使用指定的清單,初始化 BindingList<T> 類別的新實例。 |
屬性
AllowEdit |
取得或設定值,指出是否可以編輯清單中的專案。 |
AllowNew |
取得或設定值,指出您是否可以使用 AddNew() 方法將專案新增至清單。 |
AllowRemove |
取得或設定值,指出您是否可以從集合中移除專案。 |
Count |
取得實際包含在 Collection<T>中的項目數目。 (繼承來源 Collection<T>) |
IsSortedCore |
取得值,指出清單是否排序。 |
Item[Int32] |
取得或設定位於指定索引處的專案。 (繼承來源 Collection<T>) |
Items |
取得 Collection<T>周圍的 IList<T> 包裝函式。 (繼承來源 Collection<T>) |
RaiseListChangedEvents |
取得或設定值,指出在清單中新增或移除專案是否會引發 ListChanged 事件。 |
SortDirectionCore |
取得清單排序的方向。 |
SortPropertyCore |
取得在衍生類別中實作排序時,用於排序列表的屬性描述項;否則,會傳回 |
SupportsChangeNotificationCore |
取得值,指出是否啟用 ListChanged 事件。 |
SupportsSearchingCore |
取得值,指出清單是否支持搜尋。 |
SupportsSortingCore |
取得值,指出清單是否支援排序。 |
方法
事件
AddingNew |
發生於專案新增至清單之前。 |
ListChanged |
當清單或清單中的項目變更時發生。 |