PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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