ObjectSelectionWrapper.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:ObjectSelectionWrapper.cs
  5. * 2.功能描述:类文件
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/09/29 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.Reflection;
  14. namespace Dongke.IBOSS.PRD.Basics.BaseControls
  15. {
  16. /// <summary>
  17. /// Used together with the ListSelectionWrapper in order to wrap data sources for a CheckBoxComboBox.
  18. /// It helps to ensure you don't add an extra "Selected" property to a class that don't really need or want that information.
  19. /// </summary>
  20. public class ObjectSelectionWrapper<TItem, TValue>
  21. {
  22. public ObjectSelectionWrapper(TItem item, ListSelectionWrapper<TItem, TValue> container)
  23. : base()
  24. {
  25. _Container = container;
  26. _Item = item;
  27. }
  28. #region PRIVATE PROPERTIES
  29. /// <summary>
  30. /// Used as a count indicator for the item. Not necessarily displayed.
  31. /// </summary>
  32. private int _Count = 0;
  33. /// <summary>
  34. /// Is this item selected.
  35. /// </summary>
  36. private bool _Selected = false;
  37. /// <summary>
  38. /// A reference to the wrapped item.
  39. /// </summary>
  40. private TItem _Item;
  41. /// <summary>
  42. /// The containing list for these selections.
  43. /// </summary>
  44. private ListSelectionWrapper<TItem, TValue> _Container;
  45. #endregion
  46. #region PUBLIC PROPERTIES
  47. /// <summary>
  48. /// An indicator of how many items with the specified status is available for the current filter level.
  49. /// Thaught this would make the app a bit more user-friendly and help not to miss items in Statusses
  50. /// that are not often used.
  51. /// </summary>
  52. public int Count
  53. {
  54. get { return _Count; }
  55. set { _Count = value; }
  56. }
  57. /// <summary>
  58. /// A reference to the item wrapped.
  59. /// </summary>
  60. public TItem Item
  61. {
  62. get { return _Item; }
  63. //set { _Item = value; }
  64. }
  65. public TValue Value
  66. {
  67. get
  68. {
  69. TValue Value = default(TValue);
  70. if (string.IsNullOrEmpty(_Container.ValueProperty))
  71. { //Value = Item.ToString();
  72. }
  73. else if (Item is DataRow) // A specific implementation for DataRow
  74. {
  75. var v = ((DataRow)((Object)Item))[_Container.ValueProperty];
  76. Value = (v is TValue) ? (TValue)v : Value;
  77. }
  78. else
  79. {
  80. bool isFind = false;
  81. PropertyDescriptorCollection PDs = TypeDescriptor.GetProperties(Item);
  82. foreach (PropertyDescriptor PD in PDs)
  83. if (PD.Name.CompareTo(_Container.ValueProperty) == 0)
  84. {
  85. var v = PD.GetValue(Item);
  86. Value = (v is TValue) ? (TValue)v : Value;
  87. isFind = true;
  88. break;
  89. }
  90. if (!isFind)
  91. {
  92. PropertyInfo PI = Item.GetType().GetProperty(_Container.ValueProperty);
  93. if (PI == null)
  94. throw new Exception(String.Format(
  95. "Property {0} cannot be found on {1}.",
  96. _Container.DisplayNameProperty,
  97. Item.GetType()));
  98. var v = PI.GetValue(Item, null);
  99. Value = (v is TValue) ? (TValue)v : Value;
  100. }
  101. }
  102. return Value;
  103. }
  104. }
  105. /// <summary>
  106. /// The item display value. If ShowCount is true, it displays the "Name [Count]".
  107. /// </summary>
  108. public string Name
  109. {
  110. get
  111. {
  112. string Name = null;
  113. if (string.IsNullOrEmpty(_Container.DisplayNameProperty))
  114. Name = Item.ToString();
  115. else if (Item is DataRow) // A specific implementation for DataRow
  116. Name = ((DataRow)((Object)Item))[_Container.DisplayNameProperty].ToString();
  117. else
  118. {
  119. bool isFind = false;
  120. PropertyDescriptorCollection PDs = TypeDescriptor.GetProperties(Item);
  121. foreach (PropertyDescriptor PD in PDs)
  122. if (PD.Name.CompareTo(_Container.DisplayNameProperty) == 0)
  123. {
  124. Name = (string)PD.GetValue(Item).ToString();
  125. isFind = true;
  126. break;
  127. }
  128. if (!isFind)
  129. {
  130. PropertyInfo PI = Item.GetType().GetProperty(_Container.DisplayNameProperty);
  131. if (PI == null)
  132. throw new Exception(String.Format(
  133. "Property {0} cannot be found on {1}.",
  134. _Container.DisplayNameProperty,
  135. Item.GetType()));
  136. Name = PI.GetValue(Item, null).ToString();
  137. }
  138. }
  139. return _Container.ShowCounts ? String.Format("{0} [{1}]", Name, Count) : Name;
  140. }
  141. }
  142. /// <summary>
  143. /// The textbox display value. The names concatenated.
  144. /// </summary>
  145. public string NameConcatenated
  146. {
  147. get { return _Container.SelectedNames; }
  148. }
  149. public TValue[] ValueConcatenated
  150. {
  151. get
  152. {
  153. return _Container.SelectedValues;
  154. }
  155. }
  156. /// <summary>
  157. /// Indicates whether the item is selected.
  158. /// </summary>
  159. public bool Selected
  160. {
  161. get { return _Selected; }
  162. set { _Selected = value; }
  163. }
  164. #endregion
  165. }
  166. }