微软在2.0增加BindingList并实现了IbindingList
面向对象中集合类一般都会实现接口IbindingList, 因为 ,在绑定数据源的时候,如果数据源实现了IbindingList 那么界面可以与之实行互动。无意中发现了微软在2.0增加了一个新类,BindingList<T>,这个类从Collection<T>断承,并实现了IbindingList.
IbindingList 的魅力之处就在于他有AddNew,ApplySort,ListChangedEventHandler 等方法。而BindingSource 是控件的数据源和真正的数据源之间的桥梁,它可以调用IbindingList 的数AddNew等方法。同时IbindingList有数据发生改变的时候又会通知BindingSource从而更新界面。
MS 的 BindingList<T>还不支持sort ,search.,这是因为不知T为何东东有关系,要想实现些功能只有自己扩展了。BindingList<T>有点遗憾的是没有记下删除的数据,这与功能强大的表还是无法相比。从面向对象都已经到面向方面了,怎么在基础类中对面向对象支持还是不太完美呢。现在在研究,对象实体,集合,欢迎各位大师前来指教。
下面是MSDN上对BindingListr 的说明,代码示例演示如何绑定到一个包含业务对象的 BindingList 组件。
http://msdn2.microsoft.com/zh-cn/library/ms132679.aspx#Mtps_DropDownFilterTextBindingList 类可以用作创建双向数据绑定机制的基类。BindingList 提供 IBindingList 接口的具体泛型实现。这样就不必实现完整的 IBindingList 接口,实现完整接口可能会因 IBindingList、IEditableObject 和关联的 CurrencyManager 之间微妙的交互而变得比较困难。不过,典型的解决方案程序员将使用提供数据绑定功能的类(如 BindingSource),而不是直接使用 BindingList。
BindingList 通过可扩展的 AddNew 方法支持工厂创建的实例。(在 BindingSource 等其他类中也存在这种类型的扩展性)此外,由于此类实现 ICancelAddNew 接口,因此它通过 EndNew 和 CancelNew 方法实现新项的事务性提交或回滚。
注意 |
应用于此类的 HostProtectionAttribute 属性 (attribute) 拥有以下 Resources 属性 (property) 值:SharedState。HostProtectionAttribute 不影响桌面应用程序(这些应用程序通常通过双击图标、键入命令或在浏览器中输入 URL 来启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护属性。 |
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; }
}
}
}