/Main/src/DynamicDataDisplay/Common/Auxiliary/ValueStore.cs
C# | 106 lines | 86 code | 20 blank | 0 comment | 1 complexity | 2d71e29883b7554628a2c1ae39dde301 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.ComponentModel; 6using System.Diagnostics; 7 8namespace 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}