void TestEnumerationOfAttachedProperties()
5: int count = 0;
6: Y y = 7: X.SetA(y,96);"> 8: X.SetB(y,128);">"Hi");
10: //根据DependencyObject得到所有本地值
11: LocalValueEnumerator e = y.GetLocalValueEnumerator();
while (e.MoveNext()) {
13: count++;
14: if (e.Current.Property == X.AProperty)
15: Assert.AreEqual(e.Current.Value,2);
16: else if (e.Current.Property == X.BProperty)
17: Assert.AreEqual(e.Current.Value,128);">"Hi");
18: else
19: Assert.Fail("Wrong sort of property" + e.Current.Property);
20: }
21: //count为2
22: Assert.AreEqual(2,count);
23: }
@H_using System.Collections.Generic;
2: //using System.Windows.Threading;
3:
4: namespace System.Windows
5: {
6: class DependencyObject
7: {
8: //依赖属性其实终究要DependencyObject和DependencyProperty成对才能算得上真正的DependencyProperty
9: static Dictionary<Type,Dictionary<string,DependencyProperty>> propertyDeclarations = new Dictionary<Type,DependencyProperty>>();
//该依赖属性的键值对,键为DependencyProperty,值为object
11: private Dictionary<DependencyProperty,255);">object> properties = new Dictionary<DependencyProperty,255);">object>();
12:
13: //是否已密封,没有实现DependencyObject层次的IsSealed判断
14: bool IsSealed {
15: get { false; }
16: }
17:
18: //获取该DependencyObject的DependencyObjectType
19: public DependencyObjectType DependencyObjectType {
20: get { return DependencyObjectType.FromSystemType (GetType()); }
21: }
22:
23: //根据该依赖属性名,清除它的值
void ClearValue(DependencyProperty dp)
25: {
26: if (IsSealed)
27: throw new InvalidOperationException ("Cannot manipulate property values on a sealed DependencyObject");
29: properties[dp] = null;
30: }
31:
32: //根据该依赖属性DependencyPropertyKey,清除它的值
33: void ClearValue(DependencyPropertyKey key)
34: {
35: ClearValue (key.DependencyProperty);
36: }
37:
38: //根据该依赖属性名,强制值
39: void CoerceValue (DependencyProperty dp)
40: {
41: PropertyMetadata pm = dp.GetMetadata (this);
42: if (pm.CoerceValueCallback != null)
43: pm.CoerceValueCallback (this,GetValue (dp));
44: }
45:
46: sealed override bool Equals (object obj)
47: {
48: new NotImplementedException("Equals");
49: }
50:
51: int GetHashCode ()
52: {
53: "GetHashCode");
54: }
55:
56: //得到本地值的枚举器
57: public LocalValueEnumerator GetLocalValueEnumerator()
58: {
59: new LocalValueEnumerator(properties);
60: }
61:
62: //根据依赖属性名获取值
63: object GetValue(DependencyProperty dp)
64: {
65: object val = properties[dp];
66: return val == null ? dp.DefaultMetadata.DefaultValue : val;
67: }
68:
69:
70: void InvalidateProperty(DependencyProperty dp)
71: {
72: "InvalidateProperty(DependencyProperty dp)");
73: }
74:
75: //当属性值改变时,触发回调
76: protected virtual void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
77: {
78: PropertyMetadata pm = e.Property.GetMetadata (this);
79: if (pm.PropertyChangedCallback != null)
80: pm.PropertyChangedCallback ( 81: }
82:
83: //提供一个外界查看LocalValue的接口
84: object ReadLocalValue(DependencyProperty dp)
85: {
86: object val = properties[dp];
87: null ? DependencyProperty.UnsetValue : val;
88: }
89:
90: //根据依赖属性名设置其值
91: void SetValue(DependencyProperty dp,255);">value)
92: {
93: if (IsSealed)
94: "Cannot manipulate property values on a sealed DependencyObject");
95:
96: if (!dp.IsValidType (value))
97: new ArgumentException ("value not of the correct type for this DependencyProperty");
98:
99: ValidateValueCallback validate = dp.ValidateValueCallback;
100: if (validate != null && !validate(value))
101: new Exception("Value does not validate");
102: else
103: properties[dp] = value;
104: }
105:
106: //根据依赖属性DependencyPropertyKey设置其值
107: void SetValue(DependencyPropertyKey key,255);">value)
108: {
109: SetValue (key.DependencyProperty,255);">value);
110: }
111:
112: bool ShouldSerializeProperty (DependencyProperty dp)
113: {
114: new NotImplementedException ();
115: }
116:
117: //这里的注册实质是关联propertyDeclarations
118: internal void register(Type t,DependencyProperty dp)
119: {
120: if (!propertyDeclarations.ContainsKey (t))
121: propertyDeclarations[t] = new Dictionary< 122: Dictionary< 123: if (!typeDeclarations.ContainsKey(dp.Name))
124: {
125: typeDeclarations[dp.Name] = dp;
126: //这里仍然有一些问题,期待各位共同探讨解决
127: }
128: else
129: new ArgumentException("A property named " + dp.Name + " already exists on " + t.Name);
130: }
131: }
132: }
@H_