LayoutBoxDataBinding.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. 
  2. using System;
  3. using System.ComponentModel;
  4. using System.Windows.Forms;
  5. namespace Dongke.WinForm.Controls.InvoiceLayout
  6. {
  7. internal class LayoutBoxDataBinding
  8. {
  9. #region 成员变量
  10. private CurrencyManager _currencyManager; // 对象列表管理器
  11. private LayoutBox _owner; // Layout
  12. private PropertyDescriptorCollection _props; // 基本属性的描述列表集合
  13. private BindingSource _bindingSource; // 封装形式的数据源
  14. #endregion 成员变量
  15. #region 属性
  16. /// <summary>
  17. /// 获取封装形式的数据源。
  18. /// </summary>
  19. public BindingSource BindingSource
  20. {
  21. get
  22. {
  23. return _bindingSource;
  24. }
  25. }
  26. #endregion 属性
  27. #region 构造函数
  28. /// <summary>
  29. /// 构造函数
  30. /// </summary>
  31. /// <param name="owner">Layout</param>
  32. public LayoutBoxDataBinding(LayoutBox owner)
  33. {
  34. _owner = owner;
  35. _bindingSource = new BindingSource();
  36. }
  37. #endregion 构造函数
  38. #region 函数
  39. #region 公共函数
  40. /// <summary>
  41. /// 清理所有正在使用的资源
  42. /// </summary>
  43. public void Dispose()
  44. {
  45. UnWireEvents();
  46. _currencyManager = null;
  47. _owner = null;
  48. _props = null;
  49. _bindingSource = null;
  50. }
  51. /// <summary>
  52. /// 绑定数据源到文本Item
  53. /// </summary>
  54. /// <param name="textItem">文本Item</param>
  55. /// <returns>
  56. /// true : 绑定成功
  57. /// false : 绑定失败
  58. /// </returns>
  59. public bool BoundFieldToTextItem(TextItem textItem)
  60. {
  61. if (textItem == null)
  62. {
  63. return false;
  64. }
  65. DataBoundField boundField = textItem.BoundField;
  66. boundField.IsDataBound = false;
  67. boundField.BoundFieldIndex = -1;
  68. if ((_bindingSource == null)
  69. || (_props == null)
  70. || (_bindingSource.DataSource == null))
  71. {
  72. textItem.DataValue = null;
  73. textItem.BoundField = boundField;
  74. return false;
  75. }
  76. // 获取绑定数据源的索引
  77. int index = GetBoundFieldIndex(textItem.DataMember);
  78. if (index < 0)
  79. {
  80. textItem.DataValue = null;
  81. textItem.BoundField = boundField;
  82. return false;
  83. }
  84. else
  85. {
  86. boundField.IsDataBound = true;
  87. boundField.BoundFieldIndex = index;
  88. boundField.BoundFieldConverter = _props[index].Converter;
  89. boundField.ValueType = _props[index].PropertyType;
  90. textItem.BoundField = boundField;
  91. // 获取数据源中的值
  92. textItem.DataValue = GetValue(textItem, _bindingSource.Position);
  93. return true;
  94. }
  95. }
  96. /// <summary>
  97. /// 设置数据源的值到文本Item中
  98. /// </summary>
  99. /// <param name="textItem">文本Item</param>
  100. /// <returns>
  101. /// true : 设置成功
  102. /// false : 设置失败
  103. /// </returns>
  104. public bool BoundDataValueToTextItem(TextItem textItem)
  105. {
  106. if (textItem == null)
  107. {
  108. return false;
  109. }
  110. if ((!textItem.BoundField.IsDataBound)
  111. || (textItem.BoundField.BoundFieldIndex < 0)
  112. || (_props == null))
  113. {
  114. textItem.DataValue = null;
  115. return false;
  116. }
  117. textItem.DataValue = GetValue(textItem, _bindingSource.Position);
  118. return true;
  119. }
  120. /// <summary>
  121. /// 绑定数据连接设置
  122. /// </summary>
  123. /// <param name="dataSource">数据源</param>
  124. /// <param name="dataMember">数据源列表的名称</param>
  125. public void SetDataConnection(object dataSource, string dataMember)
  126. {
  127. // 卸载事件
  128. UnWireEvents();
  129. if (dataMember == null)
  130. {
  131. dataMember = string.Empty;
  132. }
  133. _bindingSource.DataSource = dataSource;
  134. _bindingSource.DataMember = dataMember;
  135. _currencyManager = _bindingSource.CurrencyManager;
  136. if (_currencyManager != null)
  137. {
  138. _props = _currencyManager.GetItemProperties();
  139. }
  140. else
  141. {
  142. _props = null;
  143. }
  144. // 加载事件
  145. WireEvents();
  146. }
  147. /// <summary>
  148. /// 成员是否包含在数据源中
  149. /// </summary>
  150. /// <param name="newDataSource">数据源</param>
  151. /// <returns>
  152. /// true : 包含
  153. /// false : 不包含
  154. /// </returns>
  155. public bool ContainsDataMember(object newDataSource)
  156. {
  157. if (newDataSource != null)
  158. {
  159. if (_currencyManager == null || _props == null)
  160. {
  161. return false;
  162. }
  163. if (!string.IsNullOrEmpty(_bindingSource.DataMember)
  164. && _props[_bindingSource.DataMember] != null)
  165. {
  166. return false;
  167. }
  168. }
  169. return true;
  170. }
  171. #endregion 公共函数
  172. #region 私有函数
  173. /// <summary>
  174. /// 取得绑定数据源的索引
  175. /// </summary>
  176. /// <param name="dataFieldName">数据字段名</param>
  177. /// <returns>
  178. /// -1 : 数据字段名不存在
  179. /// 大于-1 : 数据字段对应的索引
  180. /// </returns>
  181. private int GetBoundFieldIndex(string dataFieldName)
  182. {
  183. if (_props == null || string.IsNullOrEmpty(dataFieldName))
  184. {
  185. return -1;
  186. }
  187. for (int i = 0; i < _props.Count; i++)
  188. {
  189. if (string.Compare(_props[i].Name, dataFieldName, true,
  190. System.Globalization.CultureInfo.InvariantCulture) == 0)
  191. {
  192. return i;
  193. }
  194. }
  195. return -1;
  196. }
  197. /// <summary>
  198. /// ListChanged事件
  199. /// </summary>
  200. /// <param name="sender">指定的对象</param>
  201. /// <param name="e">提供的事件数据</param>
  202. private void bindingSource_ListChanged(object sender, ListChangedEventArgs e)
  203. {
  204. if (((e.ListChangedType == ListChangedType.PropertyDescriptorAdded)
  205. || (e.ListChangedType == ListChangedType.PropertyDescriptorDeleted))
  206. || (e.ListChangedType == ListChangedType.PropertyDescriptorChanged))
  207. {
  208. if (_currencyManager != null)
  209. {
  210. _props = _currencyManager.GetItemProperties();
  211. _owner.RefreshItemsBound();
  212. }
  213. }
  214. else
  215. {
  216. _owner.RefreshItems();
  217. }
  218. }
  219. /// <summary>
  220. /// PositionChanged事件
  221. /// </summary>
  222. /// <param name="sender">指定的对象</param>
  223. /// <param name="e">提供的事件数据</param>
  224. private void bindingSource_PositionChanged(object sender, EventArgs e)
  225. {
  226. _owner.RefreshItems();
  227. }
  228. /// <summary>
  229. /// 从数据源检索数据
  230. /// </summary>
  231. /// <param name="textItem">文本Item</param>
  232. /// <param name="positionIndex">索引</param>
  233. /// <returns>数据源中对应的数据</returns>
  234. private object GetValue(TextItem textItem, int positionIndex)
  235. {
  236. if (_props == null
  237. || _bindingSource == null
  238. || _bindingSource.List == null
  239. || positionIndex < 0)
  240. {
  241. return null;
  242. }
  243. return _props[textItem.BoundField.BoundFieldIndex].GetValue(
  244. _bindingSource.List[positionIndex]);
  245. }
  246. /// <summary>
  247. /// 卸载事件
  248. /// </summary>
  249. private void UnWireEvents()
  250. {
  251. if (_bindingSource != null)
  252. {
  253. _bindingSource.PositionChanged -=
  254. new EventHandler(bindingSource_PositionChanged);
  255. _bindingSource.ListChanged -=
  256. new ListChangedEventHandler(bindingSource_ListChanged);
  257. }
  258. }
  259. /// <summary>
  260. /// 加载事件
  261. /// </summary>
  262. private void WireEvents()
  263. {
  264. if (_bindingSource != null)
  265. {
  266. _bindingSource.PositionChanged +=
  267. new EventHandler(bindingSource_PositionChanged);
  268. _bindingSource.ListChanged +=
  269. new ListChangedEventHandler(bindingSource_ListChanged);
  270. }
  271. }
  272. #endregion 私有函数
  273. #endregion 函数
  274. }
  275. }