1:
2: using System.Collections.Generic;
3: namespace System.Windows
4: {
5: public sealed class DependencyProperty
6: {
7: //一个依赖属性可能有多个所有者,所以根据每个所有者都有自己的元数据
8: private Dictionary<Type,PropertyMetadata> metadataByType = new Dictionary<Type,PropertyMetadata>();
9:
10: //声明一个UnsetValue
11: public static readonly object UnsetValue = new object ();
12:
13: //依赖属性私有构造函数,作为初始化操作,每个依赖属性在注册的时候都会调用并初始化数据
14: private DependencyProperty (bool isAttached, string name, Type propertyType, Type ownerType,
15: PropertyMetadata defaultMetadata,
16: ValidateValueCallback validateValueCallback)
17: {
18: IsAttached = isAttached;
19: DefaultMetadata = (defaultMetadata == null ? new PropertyMetadata() : defaultMetadata);
20: Name = name;
21: OwnerType = ownerType;
22: PropertyType = propertyType;
23: ValidateValueCallback = validateValueCallback;
24: }
25:
26: internal bool IsAttached { get; set; }
27: public bool ReadOnly { get; private set; }
28: public PropertyMetadata DefaultMetadata { get; private set; }
29: public string Name { get; private set; }
30: public Type OwnerType { get; private set; }
31: public Type PropertyType { get; private set; }
32: public ValidateValueCallback ValidateValueCallback { get; private set; }
33:
34: //获取依赖属性的编号,暂未实现,在上一篇“WPF基础到企业应用系列7——深入剖析依赖属性”有实现,原理是在初始化的时候++
35: public int GlobalIndex {
36: get { throw new NotImplementedException (); }
37: }
38:
39: //传入ownerType增加Owner
40: public DependencyProperty AddOwner(Type ownerType)
41: {
42: return AddOwner (ownerType, null);
43: }
44:
45: //增加所有者,根据ownerType和typeMetadata
46: public DependencyProperty AddOwner(Type ownerType, PropertyMetadata typeMetadata)
47: {
48: if (typeMetadata == null) typeMetadata = new PropertyMetadata ();
49: OverrideMetadata (ownerType, typeMetadata);
50:
51: // MS seems to always return the same DependencyProperty
52: return this;
53: }
54:
55: //获取元数据,依据forType
56: public PropertyMetadata GetMetadata(Type forType)
57: {
58: if (metadataByType.ContainsKey (forType))
59: return metadataByType[forType];
60: return null;
61: }
62:
63: //获取元数据,依据该依赖属性
64: public PropertyMetadata GetMetadata(DependencyObject d)
65: {
66: if (metadataByType.ContainsKey (d.GetType()))
67: return metadataByType[d.GetType()];
68: return null;
69: }
70:
71: //获取元数据,依据dependencyObjectType
72: public PropertyMetadata GetMetadata(DependencyObjectType dependencyObjectType)
73: {
74: if (metadataByType.ContainsKey (dependencyObjectType.SystemType))
75: return metadataByType[dependencyObjectType.SystemType];
76: return null;
77: }
78:
79: //验证类型是否有效
80: public bool IsValidType(object value)
81: {
82: return PropertyType.IsInstanceOfType (value);
83: }
84:
85: //验证值是否有效
86: public bool IsValidValue(object value)
87: {
88: if (!IsValidType (value))
89: return false;
90: if (ValidateValueCallback == null)
91: return true;
92: return ValidateValueCallback (value);
93: }
94:
95: //重写元数据,使用PropertyMetadata类的DoMerge方法来操作
96: public void OverrideMetadata(Type forType, PropertyMetadata typeMetadata)
97: {
98: if (forType == null)
99: throw new ArgumentNullException ("forType");
100: if (typeMetadata == null)
101: throw new ArgumentNullException ("typeMetadata");
102:
103: if (ReadOnly)
104: throw new InvalidOperationException (String.Format ("Cannot override metadata on readonly property '{0}' without using a DependencyPropertyKey", Name));
105:
106: typeMetadata.DoMerge (DefaultMetadata, this, forType);
107: metadataByType.Add (forType, typeMetadata);
108: }
109:
110: //重写元数据,使用PropertyMetadata类的DoMerge方法来操作
111: public void OverrideMetadata (Type forType, PropertyMetadata typeMetadata, DependencyPropertyKey key)
112: {
113: if (forType == null)
114: throw new ArgumentNullException ("forType");
115: if (typeMetadata == null)
116: throw new ArgumentNullException ("typeMetadata");
117:
118: typeMetadata.DoMerge (DefaultMetadata, this, forType);
119: metadataByType.Add (forType, typeMetadata);
120: }
121:
122: public override string ToString ()
123: {
124: return Name;
125: }
126:
127: //得到哈希值,区别不同的依赖属性,Name、PropertyType、OwnerType的哈希值取异
128: public override int GetHashCode ()
129: {
130: return Name.GetHashCode() ^ PropertyType.GetHashCode() ^ OwnerType.GetHashCode();
131: }
132:
133: //注册依赖属性(参数:依赖属性名、依赖属性的Type、拥有者的Type)
134: public static DependencyProperty Register(string name, Type propertyType, Type ownerType)
135: {
136: return Register(name, propertyType, ownerType, null, null);
137: }
138:
139: //注册依赖属性(参数:依赖属性名、依赖属性的Type、拥有者的Type、元数据)
140: public static DependencyProperty Register(string name, Type propertyType, Type ownerType,
141: PropertyMetadata typeMetadata)
142: {
143: return Register(name, propertyType, ownerType, typeMetadata, null);
144: }
145:
146: //注册依赖属性(参数:依赖属性名、依赖属性的Type、拥有者的Type、元数据、验证回调委托)
147: public static DependencyProperty Register(string name, Type propertyType, Type ownerType,
148: PropertyMetadata typeMetadata,
149: ValidateValueCallback validateValueCallback)
150: {
151: if (typeMetadata == null)
152: typeMetadata = new PropertyMetadata();
153:
154: DependencyProperty dp = new DependencyProperty(false, name, propertyType, ownerType,
155: typeMetadata, validateValueCallback);
156: DependencyObject.register(ownerType, dp);
157:
158: dp.OverrideMetadata (ownerType, typeMetadata);
159:
160: return dp;
161: }
162:
163: //注册附加依赖属性(参数:依赖属性名、依赖属性的Type、拥有者的Type)
164: public static DependencyProperty RegisterAttached(string name, Type propertyType, Type ownerType)
165: {
166: return RegisterAttached(name, propertyType, ownerType, null, null);
167: }
168:
169: //注册附加依赖属性(参数:依赖属性名、依赖属性的Type、拥有者的Type、元数据)
170: public static DependencyProperty RegisterAttached(string name, Type propertyType, Type ownerType,
171: PropertyMetadata defaultMetadata)
172: {
173: return RegisterAttached(name, propertyType, ownerType, defaultMetadata, null);
174: }
175:
176: //注册附加依赖属性(参数:依赖属性名、依赖属性的Type、拥有者的Type、元数据、验证回调委托)
177: public static DependencyProperty RegisterAttached(string name, Type propertyType, Type ownerType,
178: PropertyMetadata defaultMetadata,
179: ValidateValueCallback validateValueCallback)
180: {
181: DependencyProperty dp = new DependencyProperty(true, name, propertyType, ownerType,
182: defaultMetadata, validateValueCallback);
183: DependencyObject.register(ownerType, dp);
184: return dp;
185: }
186:
187: //注册只读依赖属性,暂未实现
188: public static DependencyPropertyKey RegisterAttachedReadOnly(string name, Type propertyType, Type ownerType,
189: PropertyMetadata defaultMetadata)
190: {
191: throw new NotImplementedException("RegisterAttachedReadOnly(string name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata)");
192: }
193:
194: //注册只读依赖属性,暂未实现
195: public static DependencyPropertyKey RegisterAttachedReadOnly(string name, Type propertyType, Type ownerType,
196: PropertyMetadata defaultMetadata,
197: ValidateValueCallback validateValueCallback)
198: {
199: throw new NotImplementedException("RegisterAttachedReadOnly(string name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata, ValidateValueCallback validateValueCallback)");
200: }
201:
202: //注册只读依赖属性(参数:依赖属性名、依赖属性的Type、拥有者的Type、元数据)
203: public static DependencyPropertyKey RegisterReadOnly(string name, Type propertyType, Type ownerType,
204: PropertyMetadata typeMetadata)
205: {
206: return RegisterReadOnly (name, propertyType, ownerType, typeMetadata, null);
207: }
208:
209: //注册只读依赖属性(参数:依赖属性名、依赖属性的Type、拥有者的Type、元数据、验证回调委托)
210: public static DependencyPropertyKey RegisterReadOnly(string name, Type propertyType, Type ownerType,
211: PropertyMetadata typeMetadata,
212: ValidateValueCallback validateValueCallback)
213: {
214: DependencyProperty prop = Register (name, propertyType, ownerType, typeMetadata, validateValueCallback);
215: prop.ReadOnly = true;
216: return new DependencyPropertyKey (prop);
217: }
218:
219: }
220: }