对于数据绑定,绑定的数据源的值类型和绑定目标的依赖属性的值类型可能 会不同,系统提供了一些默认的绑定类型转换,另外也可以由用户自定义这种绑 定转换:
一、定义CLR类型
定义一个CLR类型,内部存在两个属性字符串类型的 ColorString和Color对象类型的ColorObject,供应用程序界面调用:
1: using System.ComponentModel;
2: using System.Windows.Media;
3:
4: namespace BasicWPFDataBinding
5: {
6: public class MyBindingColor : INotifyPropertyChanged
7: {
8: public event PropertyChangedEventHandler PropertyChanged;
9:
10: public MyBindingColor()
11: {
12: _ColorString = "Red";
13: _ColorObject = Colors.Red;
14: }
15:
16: // 字符串类型的ColorString
17: private string _ColorString;
18: public string ColorString
19: {
20: set
21: {
22: _ColorString = value;
23: if (PropertyChanged != null)
24: {
25: PropertyChanged(this,new PropertyChangedEventArgs("ColorString"));
26: }
27: }
28: get
29: {
30: return _ColorString;
31: }
32: }
33:
34: // Color对象类型的ColorObject
35: private Color _ColorObject;
36: public Color ColorObject
37: {
38: set
39: {
40: _ColorObject = value;
41: if (PropertyChanged != null)
42: {
43: PropertyChanged(this, new PropertyChangedEventArgs ("ColorObject"));
44: }
45: }
46: get
47: {
48: return _ColorObject;
49: }
50: }
51: }
52: }