news 2026/5/24 8:45:20

C#中BindingList的作用小结

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C#中BindingList的作用小结

在C#中,BindingList<T>是一个非常重要的集合类,位于System.ComponentModel命名空间,主要用于实现数据绑定(Data Binding)场景。

1. 核心作用

BindingList<T>List<T>的增强版,主要提供以下功能:

  • 自动通知UI更新:当集合内容变化(增删改)时,自动触发事件通知绑定控件(如DataGridView、ListBox等)刷新显示。
  • 支持双向数据绑定:简化UI控件与数据集合的同步,无需手动编写刷新逻辑。
  • 扩展的事件支持:比普通List<T>提供更多细粒度的事件(如AddingNewListChanged)。

2. 关键特性

(1) 自动触发UI更新

1

2

3

4

5

BindingList<string> names =newBindingList<string>();

dataGridView1.DataSource = names;// 绑定到DataGridView

names.Add("Alice");// 添加项时,DataGridView会自动更新显示

names.RemoveAt(0);// 删除项时,UI同步更新

(2) 丰富的事件

事件触发时机
ListChanged列表内容或结构变化时(增删改排序等)
AddingNew添加新项之前
AddingNew添加新项之前

1

2

3

4

names.ListChanged += (sender, e) =>

{

Console.WriteLine($"列表已更改,类型: {e.ListChangedType}");

};

(3) 支持编辑通知

T实现INotifyPropertyChanged,项属性修改时也会通知UI:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

publicclassPerson : INotifyPropertyChanged

{

privatestring_name;

publicstringName

{

get=> _name;

set{ _name = value; OnPropertyChanged(nameof(Name)); }

}

publiceventPropertyChangedEventHandler? PropertyChanged;

protectedvoidOnPropertyChanged(stringpropertyName) =>

PropertyChanged?.Invoke(this,newPropertyChangedEventArgs(propertyName));

}

// 使用

BindingList<Person> people =newBindingList<Person>();

dataGridView1.DataSource = people;

people.Add(newPerson { Name ="Bob"});

people[0].Name ="Alice";// 修改属性时,UI自动更新!

3. 典型使用场景

(1) WinForms/WPF数据绑定

1

2

3

4

5

6

// WinForms示例

BindingList<Product> products =newBindingList<Product>();

dataGridView1.DataSource = products;

// WPF示例(需配合ObservableCollection,但BindingList在某些场景仍有用)

listBox.ItemsSource = products;

复制讲解

(2) 实时监控集合变化

1

2

3

var logs =newBindingList<string>();

logs.ListChanged += (s, e) => Console.WriteLine($"日志变更: {logs[e.NewIndex]}");

logs.Add("系统启动");// 触发事件

4. 注意事项

  • 性能:频繁大规模更新时,考虑使用ResetItems通知而非逐项更新。
  • 线程安全:需通过Invoke在UI线程操作(与所有控件交互一样)。
  • WPF优先用ObservableCollection<T>BindingList主要面向WinForms设计。
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/24 8:44:16

Warcraft Helper终极指南:让魔兽争霸3在现代系统焕发新生

Warcraft Helper终极指南&#xff1a;让魔兽争霸3在现代系统焕发新生 【免费下载链接】WarcraftHelper Warcraft III Helper , support 1.20e, 1.24e, 1.26a, 1.27a, 1.27b 项目地址: https://gitcode.com/gh_mirrors/wa/WarcraftHelper 还在为魔兽争霸3在Windows 10/11…

作者头像 李华
网站建设 2026/5/24 8:41:28

XXMI启动器:游戏模组管理的终极智能革命

XXMI启动器&#xff1a;游戏模组管理的终极智能革命 【免费下载链接】XXMI-Launcher Modding platform for GI, HSR, WW and ZZZ 项目地址: https://gitcode.com/gh_mirrors/xx/XXMI-Launcher 还在为游戏模组管理而烦恼吗&#xff1f;每次想给心爱的角色换个新造型&…

作者头像 李华
网站建设 2026/5/24 8:40:39

给 Agent 起名的艺术

《给 Agent 起名的艺术&#xff1a;从技术辨识度到情感共鸣&#xff0c;让你的 AI 伙伴“开口即让人记住”》 副标题&#xff1a;大模型时代的AI身份构建方法论&#xff0c;附百万级曝光命名的公式、避坑指南与多场景命名库 引言 痛点引入 你有没有过这种经历&#xff1f; 熬…

作者头像 李华
网站建设 2026/5/24 8:38:34

JetBrains IDE试用期重置终极指南:三步轻松恢复30天试用

JetBrains IDE试用期重置终极指南&#xff1a;三步轻松恢复30天试用 【免费下载链接】ide-eval-resetter 项目地址: https://gitcode.com/gh_mirrors/id/ide-eval-resetter 你是否曾因JetBrains IDE试用期到期而苦恼&#xff1f;ide-eval-resetter正是解决这一痛点的终…

作者头像 李华