前言 :
在文章「[.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();
}
}
}