BindingSource 类定义
-
程序集: System.Windows.Forms.dll -
C# [System.ComponentModel.ComplexBindingProperties("DataSource", "DataMember")]
public class BindingSource : System.ComponentModel.Component, System.Collections.IList, System.ComponentModel.IBindingListView, System.ComponentModel.ICancelAddNew, System.ComponentModel.ISupportInitializeNotification, System.ComponentModel.ITypedList, System.Windows.Forms.ICurrencyManagerProvider
示例
下面的代码示例演示绑定到 BindingSource的 ListBox。 BindingSource 绑定到包含字体列表的 BindingList<T>。 C# using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace BindingSourceExamples
{
public class Form1 : Form
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
public Form1()
{
this.Load += new EventHandler(Form1_Load);
}
private TextBox textBox1;
private Button button1;
private ListBox listBox1;
private BindingSource binding1;
void Form1_Load(object sender, EventArgs e)
{
listBox1 = new ListBox();
textBox1 = new TextBox();
binding1 = new BindingSource();
button1 = new Button();
listBox1.Location = new Point(140, 25);
listBox1.Size = new Size(123, 160);
textBox1.Location = new Point(23, 70);
textBox1.Size = new Size(100, 20);
textBox1.Text = "Wingdings";
button1.Location = new Point(23, 25);
button1.Size = new Size(75, 23);
button1.Text = "Search";
button1.Click += new EventHandler(this.button1_Click);
this.ClientSize = new Size(292, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.listBox1);
MyFontList fonts = new MyFontList();
for (int i = 0; i < FontFamily.Families.Length; i++)
{
if (FontFamily.Families[i].IsStyleAvailable(FontStyle.Regular))
fonts.Add(new Font(FontFamily.Families[i], 11.0F, FontStyle.Regular));
}
binding1.DataSource = fonts;
listBox1.DataSource = binding1;
listBox1.DisplayMember = "Name";
}
private void button1_Click(object sender, EventArgs e)
{
if (binding1.SupportsSearching != true)
{
MessageBox.Show("Cannot search the list.");
}
else
{
int foundIndex = binding1.Find("Name", textBox1.Text);
if (foundIndex > -1)
listBox1.SelectedIndex = foundIndex;
else
MessageBox.Show("Font was not found.");
}
}
}
public class MyFontList : BindingList<Font>
{
protected override bool SupportsSearchingCore
{
get { return true; }
}
protected override int FindCore(PropertyDescriptor prop, object key)
{
// Ignore the prop value and search by family name.
for (int i = 0; i < Count; ++i)
{
if (Items[i].FontFamily.Name.ToLower() == ((string)key).ToLower())
return i;
}
return -1;
}
}
}
注解
BindingSource 组件提供多种用途。 首先,通过在 Windows 窗体控件和数据源之间提供货币管理、更改通知和其他服务,简化了窗体上的控件与数据的绑定。 这是通过使用 DataSource 属性将 BindingSource 组件附加到数据源来实现的。 对于复杂的绑定方案,可以选择将 DataMember 属性设置为数据源中的特定列或列表。 然后,将控件绑定到 BindingSource。 与数据的所有进一步交互都是通过调用 BindingSource 组件来实现的。 有关 BindingSource 如何简化绑定过程的示例,请参阅如何:将 Windows 窗体控件绑定到 DBNull 数据库值和如何:处理数据绑定发生的错误和异常。 数据源的导航和更新通过 MoveNext、MoveLast和 Remove等方法来完成。 排序和筛选等操作通过 Sort 和 Filter 属性进行处理。 有关将排序和筛选用于 BindingSource的详细信息,请参阅如何:使用 Windows 窗体 BindingSource 组件对 ADO.NET 数据进行排序和筛选。 此外,BindingSource 组件可以充当强类型的数据源。 通常,基础数据源的类型是通过以下机制之一固定的: 这两种机制都创建强类型列表。 有关如何使用 BindingSource 绑定到类型的详细信息,请参阅如何:将 Windows 窗体控件绑定到类型。 你还可以使用 BindingSource 将控件绑定到工厂对象。 有关如何执行此操作的详细信息,请参阅如何:将 Windows 窗体控件绑定到工厂对象。 备注 由于 BindingSource 处理简单和复杂的数据源,因此术语是有问题的。 在此类文档中,术语 "列表" 指的是托管数据源中的数据集合,"项" 表示单个元素。 讨论与复杂数据源关联的功能时,将使用等效术语表和行。 BindingSource 提供访问基础数据的成员。 可以通过 Current 属性检索当前项,整个列表可以通过 List 属性进行检索。 Current 和 RemoveCurrent、EndEdit、CancelEdit 以及 Add 和 AddNew 方法,当前项支持编辑操作。 尽管自动为所有基础数据源类型处理币种管理,但此类公开了许多事件,例如 CurrentItemChanged 和 DataSourceChanged,它们允许进行自定义。 绑定到 BindingSource 组件的数据源还可以使用 BindingNavigator 类进行导航和管理,该类提供类似 VCR 的用户界面(UI)用于在列表中导航项。 尽管 BindingNavigator 可以绑定到任何数据源,但它旨在通过其 BindingNavigator.BindingSource 属性与 BindingSource 组件集成。 BindingSource 类的默认属性为 DataSource。 默认事件是 CurrentChanged。
构造函数
属性
方法
事件
显式接口实现
扩展方法
适用于.NET
5 Preview 1 .NET Core
3.1 3.0 .NET Framework
4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6 4.5.2 4.5.1 4.5 4.0 3.5 3.0 2.0
|