[Windows Forms] : BindingSource使用模式 - Data Binding基础知识 (一)
前言 :
在文章「[.NET] : BindingSource使用模式 - 连动式下拉选单 (纯IDE开发)」。
介绍了如何单纯使用Visual Studio的IDE来开发连动式下拉选单。
用IDE开发的模式,可以快速建立项目所需的用户接口。
但是当我们需要对细节有更高的掌控时,这开发模式会越来越不敷使用。
这时就需要改用程序代码开发的模式,来做 Data Binding的开发设计。
本篇文章简略介绍,几个设计开发 Data Binding用到的对象。
让软件开发人员在设计 Data Binding相关程序代码时,能对对象运作模式有基础的理解。
INotifyPropertyChanged :
INotifyPropertyChanged就只有定义一个事件「PropertyChanged」。
实作INotifyPropertyChanged的对象,会在属性值变更的时候引发 PropertyChanged事件。
相关资料 : INotifyPropertyChanged 界面
PropertyDescriptor :
PropertyDescriptor主要的功能,是将类别(Class)的属性(Property)做封装。
例如说有一个类别,我们要条列它所开放(Public)的属性。
这时候可以透过 System.ComponentModel.TypeDescriptor.GetProperties,来取得这个类别的 PropertyDescriptor对象集合。
这个 PropertyDescriptor对象集合里的每个PropertyDescriptor对象,就是该类别所有开放属性的封装。
开发人员可以透过 PropertyDescriptor对象,来取得该属性的相关信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | using System; using System.ComponentModel; namespace ConsoleApplication1 { public class County { // Properties public int CountyID { get ; private set ; } public string CountyName { get ; set ; } public string CountyDescription { get ; set ; } // Constructor public County( int countyID) { this .CountyID = countyID; this .CountyName = string .Empty; this .CountyDescription = string .Empty; } } class Program { static void Main( string [] args) { foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties( typeof (County))) { Console.WriteLine( string .Format( "Name={0}; IsReadOnly={1};" , propertyDescriptor.Name, propertyDescriptor.IsReadOnly)); } Console.ReadLine(); } } } |
另外开发人员也可以透过 PropertyDescriptor对象的GetValue方法,来取得该属性的对象实例的值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | using System; using System.ComponentModel; namespace ConsoleApplication1 { public class County { // Properties public int CountyID { get ; private set ; } public string CountyName { get ; set ; } public string CountyDescription { get ; set ; } // Constructor public County( int countyID) { this .CountyID = countyID; this .CountyName = string .Empty; this .CountyDescription = string .Empty; } } class Program { static void Main( string [] args) { County county = new County(1); county.CountyName = "台北市" ; county.CountyDescription = "买不起" ; foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties( typeof (County))) { Console.WriteLine( string .Format( "Name={0}; Value={1};" , propertyDescriptor.Name, propertyDescriptor.GetValue(county))); } Console.ReadLine(); } } } |
当然开发人员也可以透过 PropertyDescriptor物件的SetValue方法,来设定该属性的对象实例的值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | using System; using System.ComponentModel; namespace ConsoleApplication1 { public class County { // Properties public int CountyID { get ; private set ; } public string CountyName { get ; set ; } public string CountyDescription { get ; set ; } // Constructor public County( int countyID) { this .CountyID = countyID; this .CountyName = string .Empty; this .CountyDescription = string .Empty; } } class Program { static void Main( string [] args) { County county = new County(1); county.CountyName = "台北市" ; county.CountyDescription = "买不起" ; // GetPropertyDescriptor PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties( typeof (County))[ "CountyDescription" ]; // SetValue propertyDescriptor.SetValue(county, "有天买得起" ); // Result Console.WriteLine( string .Format( "Value={0};" , county.CountyDescription)); Console.ReadLine(); } } } |
PropertyDescriptor对象也实做了,属性的值变更时通知的功能。
开发人员可以透过 AddValueChanged,将属性的值变更时要执行的委派加入。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | using System; using System.ComponentModel; namespace ConsoleApplication1 { public class County { // Properties public int CountyID { get ; private set ; } public string CountyName { get ; set ; } public string CountyDescription { get ; set ; } // Constructor public County( int countyID) { this .CountyID = countyID; this .CountyName = string .Empty; this .CountyDescription = string .Empty; } } class Program { static void Main( string [] args) { County county = new County(1); county.CountyName = "台北市" ; county.CountyDescription = "买不起" ; // GetPropertyDescriptor PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties( typeof (County))[ "CountyDescription" ]; // AddValueChanged EventHandler valueChangedDelegate = delegate ( object sender, EventArgs e) { Console.WriteLine( string .Format( "ValueChanged Value={0};" , ((County)sender).CountyDescription)); }; propertyDescriptor.AddValueChanged(county, valueChangedDelegate); // SetValue propertyDescriptor.SetValue(county, "有天买得起" ); // Result Console.WriteLine( string .Format( "Value={0};" , county.CountyDescription)); Console.ReadLine(); } } } |
另外需要值得一提的是,如果类别实做了INotifyPropertyChanged接口。
当程序引发了INotifyPropertyChanged.PropertyChanged事件时,透过 AddValueChanged加入的委派也将会被执行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | using System; using System.ComponentModel; namespace ConsoleApplication1 { public class County : INotifyPropertyChanged { // Properties private int _countyID = int .MinValue; private string _countyName = string .Empty; private string _countyDescription = string .Empty; // Constructor public County( int countyID) { _countyID = countyID; _countyName = string .Empty; _countyDescription = string .Empty; } // Properties public int CountyID { get { return _countyID; } set { if (_countyID != value) { _countyID = value; this .OnNotifyPropertyChanged( "CountyID" ); } } } public string CountyName { get { return _countyName; } set { if (_countyName != value) { _countyName = value; this .OnNotifyPropertyChanged( "CountyName" ); } } } public string CountyDescription { get { return _countyDescription; } set { if (_countyDescription != value) { _countyDescription = value; this .OnNotifyPropertyChanged( "CountyDescription" ); } } } // Event public event PropertyChangedEventHandler PropertyChanged; protected void OnNotifyPropertyChanged( string propertyName) { #region Require if ( string .IsNullOrEmpty(propertyName) == true ) throw new ArgumentNullException(); #endregion if ( this .PropertyChanged != null ) { this .PropertyChanged( this , new PropertyChangedEventArgs(propertyName)); } } } class Program { static void Main( string [] args) { County county = new County(1); county.CountyName = "台北市" ; county.CountyDescription = "买不起" ; // GetPropertyDescriptor PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties( typeof (County))[ "CountyDescription" ]; // AddValueChanged EventHandler valueChangedDelegate = delegate ( object sender, EventArgs e) { Console.WriteLine( string .Format( "ValueChanged Value={0};" , ((County)sender).CountyDescription)); }; propertyDescriptor.AddValueChanged(county, valueChangedDelegate); // SetValue county.CountyDescription = "有天买得起" ; // Result Console.WriteLine( string .Format( "Value={0};" , county.CountyDescription)); Console.ReadLine(); } } } |
期許自己~
能以更簡潔的文字與程式碼,傳達出程式設計背後的精神。
真正做到「以形寫神」的境界。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?