PropertyCollection.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. namespace FWindSoft.WinForm
  8. {
  9. /// <summary>
  10. /// 属性窗格绑定类
  11. /// </summary>
  12. public class PropertyCollection : Collection<PropertyItem>, ICustomTypeDescriptor
  13. {
  14. public PropertyCollection() : base()
  15. {
  16. }
  17. protected override void ClearItems()
  18. {
  19. foreach (var propertyItem in this)
  20. {
  21. propertyItem.Owner = null;
  22. }
  23. base.ClearItems();
  24. }
  25. protected override void InsertItem(int index, PropertyItem item)
  26. {
  27. if (item == null)
  28. return;
  29. this[index].Owner = this;
  30. base.InsertItem(index, item);
  31. }
  32. protected override void RemoveItem(int index)
  33. {
  34. this[index].Owner = null;
  35. base.RemoveItem(index);
  36. }
  37. protected override void SetItem(int index, PropertyItem item)
  38. {
  39. item.Owner = this;
  40. this[index].Owner = null;
  41. base.SetItem(index, item);
  42. }
  43. public PropertyItem this[string name]
  44. {
  45. get
  46. {
  47. return this.FirstOrDefault(t=> t != null && t.Name == name);
  48. }
  49. }
  50. #region 实现接口
  51. public AttributeCollection GetAttributes()
  52. {
  53. return TypeDescriptor.GetAttributes(this, true);
  54. }
  55. public string GetClassName()
  56. {
  57. return TypeDescriptor.GetClassName(this, true);
  58. }
  59. public string GetComponentName()
  60. {
  61. return TypeDescriptor.GetComponentName(this, true);
  62. }
  63. public TypeConverter GetConverter()
  64. {
  65. return TypeDescriptor.GetConverter(this, true);
  66. }
  67. public EventDescriptor GetDefaultEvent()
  68. {
  69. return TypeDescriptor.GetDefaultEvent(this, true);
  70. }
  71. public PropertyDescriptor GetDefaultProperty()
  72. {
  73. return TypeDescriptor.GetDefaultProperty(this, true);
  74. }
  75. public object GetEditor(Type editorBaseType)
  76. {
  77. return TypeDescriptor.GetEditor(this, editorBaseType, true);
  78. }
  79. public EventDescriptorCollection GetEvents(Attribute[] attributes)
  80. {
  81. return TypeDescriptor.GetEvents(this, attributes, true);
  82. }
  83. public EventDescriptorCollection GetEvents()
  84. {
  85. return TypeDescriptor.GetEvents(this, true);
  86. }
  87. public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
  88. {
  89. ArrayList props = new ArrayList();
  90. for (int i = 0; i < this.Count; i++)
  91. {
  92. //判断属性是否显示
  93. if (this[i].IsVisible)
  94. {
  95. PropDescriptor psd = new PropDescriptor(this[i], attributes);
  96. props.Add(psd);
  97. }
  98. }
  99. PropertyDescriptor[] propArray = (PropertyDescriptor[])props.ToArray(typeof(PropDescriptor));
  100. return new PropertyDescriptorCollection(propArray);
  101. }
  102. public PropertyDescriptorCollection GetProperties()
  103. {
  104. return TypeDescriptor.GetProperties(this, true);
  105. }
  106. public object GetPropertyOwner(PropertyDescriptor pd)
  107. {
  108. return this;
  109. }
  110. #endregion
  111. }
  112. }