前言 :
在文章「[.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對象,來取得該屬性的相關資訊。
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方法,來取得該屬性的對象執行個體的值。
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方法,來設定該屬性的對象執行個體的值。
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,將屬性的值變更時要執行的委派加入。
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加入的委派也將會被執行。
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(); } }}
相關資料 : PropertyDescriptor 類別 、TypeDescriptor 類別