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