1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.ComponentModel;
|
---|
6 | using System.Diagnostics;
|
---|
7 |
|
---|
8 | namespace Microsoft.Research.DynamicDataDisplay
|
---|
9 | {
|
---|
10 | public sealed class ValueStore : CustomTypeDescriptor, INotifyPropertyChanged
|
---|
11 | {
|
---|
12 | public ValueStore(params string[] propertiesNames)
|
---|
13 | {
|
---|
14 | foreach (var propertyName in propertiesNames)
|
---|
15 | {
|
---|
16 | this[propertyName] = "";
|
---|
17 | }
|
---|
18 | }
|
---|
19 |
|
---|
20 | private Dictionary<string, object> cache = new Dictionary<string, object>();
|
---|
21 |
|
---|
22 | public object this[string propertyName]
|
---|
23 | {
|
---|
24 | get { return cache[propertyName]; }
|
---|
25 | set { SetValue(propertyName, value); }
|
---|
26 | }
|
---|
27 |
|
---|
28 | public ValueStore SetValue(string propertyName, object value)
|
---|
29 | {
|
---|
30 | cache[propertyName] = value;
|
---|
31 | PropertyChanged.Raise(this, propertyName);
|
---|
32 |
|
---|
33 | return this;
|
---|
34 | }
|
---|
35 |
|
---|
36 | private PropertyDescriptorCollection collection;
|
---|
37 | public override PropertyDescriptorCollection GetProperties()
|
---|
38 | {
|
---|
39 | PropertyDescriptor[] propertyDescriptors = new PropertyDescriptor[cache.Count];
|
---|
40 | var keys = cache.Keys.ToArray();
|
---|
41 | for (int i = 0; i < keys.Length; i++)
|
---|
42 | {
|
---|
43 | propertyDescriptors[i] = new ValueStorePropertyDescriptor(keys[i]);
|
---|
44 | }
|
---|
45 |
|
---|
46 | collection = new PropertyDescriptorCollection(propertyDescriptors);
|
---|
47 | return collection;
|
---|
48 | }
|
---|
49 |
|
---|
50 | private sealed class ValueStorePropertyDescriptor : PropertyDescriptor
|
---|
51 | {
|
---|
52 | private readonly string name;
|
---|
53 |
|
---|
54 | public ValueStorePropertyDescriptor(string name)
|
---|
55 | : base(name, null)
|
---|
56 | {
|
---|
57 | this.name = name;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public override bool CanResetValue(object component)
|
---|
61 | {
|
---|
62 | return false;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public override Type ComponentType
|
---|
66 | {
|
---|
67 | get { return typeof(ValueStore); }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public override object GetValue(object component)
|
---|
71 | {
|
---|
72 | ValueStore store = (ValueStore)component;
|
---|
73 | return store[name];
|
---|
74 | }
|
---|
75 |
|
---|
76 | public override bool IsReadOnly
|
---|
77 | {
|
---|
78 | get { return false; }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public override Type PropertyType
|
---|
82 | {
|
---|
83 | get { return typeof(string); }
|
---|
84 | }
|
---|
85 |
|
---|
86 | public override void ResetValue(object component)
|
---|
87 | {
|
---|
88 | }
|
---|
89 |
|
---|
90 | public override void SetValue(object component, object value)
|
---|
91 | {
|
---|
92 | }
|
---|
93 |
|
---|
94 | public override bool ShouldSerializeValue(object component)
|
---|
95 | {
|
---|
96 | return false;
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | #region INotifyPropertyChanged Members
|
---|
101 |
|
---|
102 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
103 |
|
---|
104 | #endregion
|
---|
105 | }
|
---|
106 | }
|
---|