LayoutBoxPrintDataBinding.cs 9.3 KB

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