分享

BindingSource 类 (System.Windows.Forms) | Microsoft Docs

 长江黄鹤 2020-04-23

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

示例

下面的代码示例演示绑定到 BindingSourceListBoxBindingSource 绑定到包含字体列表的 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 数据库值如何:处理数据绑定发生的错误和异常。 数据源的导航和更新通过 MoveNextMoveLastRemove等方法来完成。 排序和筛选等操作通过 SortFilter 属性进行处理。 有关将排序和筛选用于 BindingSource的详细信息,请参阅如何:使用 Windows 窗体 BindingSource 组件对 ADO.NET 数据进行排序和筛选

此外,BindingSource 组件可以充当强类型的数据源。 通常,基础数据源的类型是通过以下机制之一固定的:

这两种机制都创建强类型列表。 有关如何使用 BindingSource 绑定到类型的详细信息,请参阅如何:将 Windows 窗体控件绑定到类型。 你还可以使用 BindingSource 将控件绑定到工厂对象。 有关如何执行此操作的详细信息,请参阅如何:将 Windows 窗体控件绑定到工厂对象

备注

由于 BindingSource 处理简单和复杂的数据源,因此术语是有问题的。 在此类文档中,术语 "列表" 指的是托管数据源中的数据集合,"" 表示单个元素。 讨论与复杂数据源关联的功能时,将使用等效术语

BindingSource 提供访问基础数据的成员。 可以通过 Current 属性检索当前项,整个列表可以通过 List 属性进行检索。 CurrentRemoveCurrentEndEditCancelEdit 以及 AddAddNew 方法,当前项支持编辑操作。 尽管自动为所有基础数据源类型处理币种管理,但此类公开了许多事件,例如 CurrentItemChangedDataSourceChanged,它们允许进行自定义。

绑定到 BindingSource 组件的数据源还可以使用 BindingNavigator 类进行导航和管理,该类提供类似 VCR 的用户界面(UI)用于在列表中导航项。 尽管 BindingNavigator 可以绑定到任何数据源,但它旨在通过其 BindingNavigator.BindingSource 属性与 BindingSource 组件集成。

BindingSource 类的默认属性为 DataSource。 默认事件是 CurrentChanged

注意

BindingSource 类的许多成员对 List 属性所表示的基础列表进行操作,只需将其操作引用到基础列表。 因此,当 BindingSource 绑定到 IList的自定义实现时,这些成员的确切行为可能与类文档中所述的行为不同。 例如,RemoveAt 方法调用 IList.RemoveAtBindingSource 文档介绍 RemoveAt 方法,并了解底层 IListRemoveAt 方法是否正确实现。

构造函数

表 1
BindingSource()

BindingSource 类的新实例初始化为默认属性值。

BindingSource(IContainer)

初始化 BindingSource 类的新实例,并将 BindingSource 添加到指定的容器。

BindingSource(Object, String)

用指定的数据源和数据成员初始化 BindingSource 类的新实例。

属性

表 2
AllowEdit

获取一个值,该值指示是否可以编辑基础列表中的项。

AllowNew

获取或设置一个值,该值指示是否可以使用 AddNew() 方法向列表中添加项。

AllowRemove

获取一个值,它指示是否可从基础列表中移除项。

CanRaiseEvents

获取一个指示组件是否可以引发事件的值。

(继承自 Component)
Container

获取包含 IContainerComponent

(继承自 Component)
Count

获取在的基础列表中项的总数。获取在基础列表中项的总数,考虑当前 Filter 值。

CurrencyManager

获取与此 BindingSource 关联的当前项管理器。

Current

获取列表中的当前项。

DataMember

获取或设置连接器当前绑定到的数据源中的特定列表。

DataSource

获取或设置连接器绑定到的数据源。

DesignMode

获取一个值,用以指示 Component 当前是否处于设计模式。

(继承自 Component)
Events

获取附加到此 Component 的事件处理程序的列表。

(继承自 Component)
Filter

获取或设置用于筛选查看哪些行的表达式。

IsBindingSuspended

获取一个值,该值指示列表绑定是否已挂起。

IsFixedSize

获取一个值,该值指示基础列表是否具有固定大小。

IsReadOnly

获取一个值,该值指示基础列表是否为只读。

IsSorted

获取一个值,该值指示是否可以对基础列表中的项排序。

IsSynchronized

获取一个值,该值指示对集合的访问是否为同步的(线程安全)。

Item[Int32]

获取或设置指定索引处的列表元素。

List

获取连接器绑定到的列表。

Position

获取或设置基础列表中当前项的索引。

RaiseListChangedEvents

获取或设置一个值,该值指示是否应引发 ListChanged 事件。

Site

获取或设置 ISiteComponent

(继承自 Component)
Sort

获取或设置用于排序的列名称以及用于查看数据源中的行的排序顺序。

SortDescriptions

获取应用于数据源的排序说明的集合。

SortDirection

获取列表中项的排序方向。

SortProperty

获取正在用于对列表进行排序的 PropertyDescriptor

SupportsAdvancedSorting

获取一个值,它指示数据源是否支持多列排序。

SupportsChangeNotification

获取一个值,它指示数据源是否支持更改通知。

SupportsFiltering

获取一个值,该值指示数据源是否支持筛选。

SupportsSearching

获取一个值,它指示数据源是否支持使用 Find(PropertyDescriptor, Object) 方法进行搜索。

SupportsSorting

获取一个值,它指示数据源是否支持排序。

SyncRoot

获取可用于同步对基础列表的访问的对象。

方法

表 3
Add(Object)

将现有项添加到内部列表中。

AddNew()

在基础列表中添加一个新项。

ApplySort(ListSortDescriptionCollection)

使用指定的排序说明对数据源进行排序。

ApplySort(PropertyDescriptor, ListSortDirection)

使用指定的属性说明符和排序方向对数据源进行排序。

CancelEdit()

取消当前的编辑操作。

Clear()

从列表中移除所有元素。

Contains(Object)

确定某个对象是否为列表中的项。

CopyTo(Array, Int32)

List 中的内容复制到指定数组,从指定索引值处开始。

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。

(继承自 MarshalByRefObject)
Dispose()

释放由 Component 使用的所有资源。

(继承自 Component)
Dispose(Boolean)

释放由 BindingSource 占用的非托管资源,还可以另外再释放托管资源。

EndEdit()

将挂起的更改应用于基础数据源。

Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
Find(PropertyDescriptor, Object)

搜索具有指定属性描述符的项索引。

Find(String, Object)

使用指定的属性名和值返回列表中的项的索引。

GetEnumerator()

检索 List 的一个枚举数。

GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetItemProperties(PropertyDescriptor[])

检索表示数据源列表类型的可绑定属性的 PropertyDescriptor 对象的数组。

GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。

(继承自 MarshalByRefObject)
GetListName(PropertyDescriptor[])

获取为绑定提供数据的列表的名称。

GetRelatedCurrencyManager(String)

为指定的数据成员获取相关的当前项管理器。

GetService(Type)

返回一个对象,该对象表示由 Component 或它的 Container 提供的服务。

(继承自 Component)
GetType()

获取当前实例的 Type

(继承自 Object)
IndexOf(Object)

搜索指定的对象,并返回整个列表中第一个匹配项的索引。

InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。

(继承自 MarshalByRefObject)
Insert(Int32, Object)

将一项插入列表中指定的索引处。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。

(继承自 MarshalByRefObject)
MoveFirst()

移至列表中的第一项。

MoveLast()

移至列表中的最后一项。

MoveNext()

移至列表中的下一项。

MovePrevious()

移至列表中的上一项。

OnAddingNew(AddingNewEventArgs)

引发 AddingNew 事件。

OnBindingComplete(BindingCompleteEventArgs)

引发 BindingComplete 事件。

OnCurrentChanged(EventArgs)

引发 CurrentChanged 事件。

OnCurrentItemChanged(EventArgs)

引发 CurrentItemChanged 事件。

OnDataError(BindingManagerDataErrorEventArgs)

引发 DataError 事件。

OnDataMemberChanged(EventArgs)

引发 DataMemberChanged 事件。

OnDataSourceChanged(EventArgs)

引发 DataSourceChanged 事件。

OnListChanged(ListChangedEventArgs)

引发 ListChanged 事件。

OnPositionChanged(EventArgs)

引发 PositionChanged 事件。

Remove(Object)

从列表中移除指定的项。

RemoveAt(Int32)

移除此列表中指定索引处的项。

RemoveCurrent()

从列表中移除当前项。

RemoveFilter()

移除与 BindingSource 关联的筛选器。

RemoveSort()

移除与 BindingSource 关联的排序。

ResetAllowNew()

重新初始化 AllowNew 属性。

ResetBindings(Boolean)

使绑定到 BindingSource 的控件重新读取列表中的所有项,并刷新这些项的显示值。

ResetCurrentItem()

使绑定到 BindingSource 的控件重新读取当前选定的项,并刷新其显示值。

ResetItem(Int32)

使绑定到 BindingSource 的控件重新读取指定索引处的项,并刷新其显示值。

ResumeBinding()

继续数据绑定。

SuspendBinding()

挂起数据绑定,以阻止使用所做的更改对绑定数据源进行更新。

ToString()

返回包含 String 的名称的 Component(如果有)。 不应重写此方法。

(继承自 Component)

事件

表 4
AddingNew

在将项添加到基础列表之前发生。

BindingComplete

当所有客户端都已绑定到此 BindingSource 时发生。

CurrentChanged

在当前绑定项更改时发生。

CurrentItemChanged

Current 属性的属性值更改后发生。

DataError

当货币相关的异常由 BindingSource 无提示处理时发生。

DataMemberChanged

DataMember 属性值更改后发生。

DataSourceChanged

DataSource 属性值更改后发生。

Disposed

在通过调用 Dispose() 方法释放组件时发生。

(继承自 Component)
ListChanged

当基础列表更改或列表中的项更改时发生。

PositionChanged

Position 属性的值更改后发生。

显式接口实现

表 5
IBindingList.AddIndex(PropertyDescriptor)

PropertyDescriptor 添加到用于搜索的索引。

IBindingList.RemoveIndex(PropertyDescriptor)

PropertyDescriptor 从用于搜索的索引中移除。

ICancelAddNew.CancelNew(Int32)

丢弃集合中挂起的新项。

ICancelAddNew.EndNew(Int32)

向集合提交挂起的新项。

ISupportInitialize.BeginInit()

用信号通知 BindingSource 初始化即将开始。

ISupportInitialize.EndInit()

用信号通知 BindingSource 初始化已完成。

ISupportInitializeNotification.Initialized

初始化 BindingSource 时出现。

ISupportInitializeNotification.IsInitialized

获取一个值,该值指示是否初始化 BindingSource

扩展方法

表 6
Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。

AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryable

适用于

.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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多