UCListView.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-22-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCListView.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.Drawing;
  20. using System.Data;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. using System.Collections;
  25. namespace HZH_Controls.Controls
  26. {
  27. /// <summary>
  28. /// Class UCListView.
  29. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  30. /// </summary>
  31. /// <seealso cref="System.Windows.Forms.UserControl" />
  32. [DefaultEvent("SelectedItemEvent")]
  33. public partial class UCListView : UserControl
  34. {
  35. /// <summary>
  36. /// The m int cell width
  37. /// </summary>
  38. int m_intCellWidth = 130;//单元格宽度
  39. /// <summary>
  40. /// The m int cell height
  41. /// </summary>
  42. int m_intCellHeight = 120;//单元格高度
  43. /// <summary>
  44. /// The m item type
  45. /// </summary>
  46. private Type m_itemType = typeof(UCListViewItem);
  47. /// <summary>
  48. /// Gets or sets the type of the item.
  49. /// </summary>
  50. /// <value>The type of the item.</value>
  51. /// <exception cref="System.Exception">单元格控件没有继承实现接口IListViewItem</exception>
  52. /// <exception cref="Exception">单元格控件没有继承实现接口IListViewItem</exception>
  53. [Description("单元格类型,如果无法满足您的需求,你可以自定义单元格控件,并实现接口IListViewItem"), Category("自定义")]
  54. public Type ItemType
  55. {
  56. get { return m_itemType; }
  57. set
  58. {
  59. if (!typeof(IListViewItem).IsAssignableFrom(value) || !value.IsSubclassOf(typeof(Control)))
  60. throw new Exception("单元格控件没有继承实现接口IListViewItem");
  61. m_itemType = value;
  62. }
  63. }
  64. /// <summary>
  65. /// The m page
  66. /// </summary>
  67. private UCPagerControlBase m_page = null;
  68. /// <summary>
  69. /// 翻页控件
  70. /// </summary>
  71. /// <value>The page.</value>
  72. /// <exception cref="System.Exception">翻页控件没有继承UCPagerControlBase</exception>
  73. /// <exception cref="Exception">翻页控件没有继承UCPagerControlBase</exception>
  74. [Description("翻页控件,如果UCPagerControl不满足你的需求,请自定义翻页控件并继承UCPagerControlBase"), Category("自定义")]
  75. public UCPagerControlBase Page
  76. {
  77. get { return m_page; }
  78. set
  79. {
  80. m_page = value;
  81. if (value != null)
  82. {
  83. if (!typeof(IPageControl).IsAssignableFrom(value.GetType()) || !value.GetType().IsSubclassOf(typeof(UCPagerControlBase)))
  84. throw new Exception("翻页控件没有继承UCPagerControlBase");
  85. this.panMain.AutoScroll = false;
  86. panPage.Visible = true;
  87. this.Controls.SetChildIndex(panMain, 0);
  88. m_page.ShowSourceChanged += m_page_ShowSourceChanged;
  89. m_page.Dock = DockStyle.Fill;
  90. this.panPage.Controls.Clear();
  91. this.panPage.Controls.Add(m_page);
  92. GetCellCount();
  93. this.DataSource = m_page.GetCurrentSource();
  94. }
  95. else
  96. {
  97. this.panMain.AutoScroll = true;
  98. m_page = null;
  99. panPage.Visible = false;
  100. }
  101. }
  102. }
  103. /// <summary>
  104. /// The m data source
  105. /// </summary>
  106. private object m_dataSource = null;
  107. /// <summary>
  108. /// Gets or sets the data source.
  109. /// </summary>
  110. /// <value>The data source.</value>
  111. /// <exception cref="System.Exception">数据源不是有效的数据类型,列表</exception>
  112. /// <exception cref="Exception">数据源不是有效的数据类型,列表</exception>
  113. [Description("数据源,如果使用翻页控件,请使用翻页控件的DataSource"), Category("自定义")]
  114. public object DataSource
  115. {
  116. get { return m_dataSource; }
  117. set
  118. {
  119. if (value == null)
  120. {
  121. m_dataSource = value;
  122. ReloadSource();
  123. return;
  124. }
  125. if (!typeof(IList).IsAssignableFrom(value.GetType()))
  126. {
  127. throw new Exception("数据源不是有效的数据类型,列表");
  128. }
  129. m_dataSource = value;
  130. ReloadSource();
  131. }
  132. }
  133. /// <summary>
  134. /// The m int cell count
  135. /// </summary>
  136. int m_intCellCount = 0;//单元格总数
  137. /// <summary>
  138. /// Gets the cell count.
  139. /// </summary>
  140. /// <value>The cell count.</value>
  141. [Description("单元格总数"), Category("自定义")]
  142. public int CellCount
  143. {
  144. get { return m_intCellCount; }
  145. private set
  146. {
  147. m_intCellCount = value;
  148. if (value > 0 && m_page != null)
  149. {
  150. m_page.PageSize = m_intCellCount;
  151. m_page.Reload();
  152. }
  153. }
  154. }
  155. /// <summary>
  156. /// The m selected source
  157. /// </summary>
  158. private List<object> m_selectedSource = new List<object>();
  159. /// <summary>
  160. /// Gets or sets the selected source.
  161. /// </summary>
  162. /// <value>The selected source.</value>
  163. [Description("选中的数据"), Category("自定义")]
  164. public List<object> SelectedSource
  165. {
  166. get { return m_selectedSource; }
  167. set
  168. {
  169. m_selectedSource = value;
  170. ReloadSource();
  171. }
  172. }
  173. /// <summary>
  174. /// The m is multiple
  175. /// </summary>
  176. private bool m_isMultiple = true;
  177. /// <summary>
  178. /// Gets or sets a value indicating whether this instance is multiple.
  179. /// </summary>
  180. /// <value><c>true</c> if this instance is multiple; otherwise, <c>false</c>.</value>
  181. [Description("是否多选"), Category("自定义")]
  182. public bool IsMultiple
  183. {
  184. get { return m_isMultiple; }
  185. set { m_isMultiple = value; }
  186. }
  187. /// <summary>
  188. /// Occurs when [selected item event].
  189. /// </summary>
  190. [Description("选中项事件"), Category("自定义")]
  191. public event EventHandler SelectedItemEvent;
  192. /// <summary>
  193. /// Delegate ReloadGridStyleEventHandle
  194. /// </summary>
  195. /// <param name="intCellCount">The int cell count.</param>
  196. public delegate void ReloadGridStyleEventHandle(int intCellCount);
  197. /// <summary>
  198. /// 样式改变事件
  199. /// </summary>
  200. [Description("样式改变事件"), Category("自定义")]
  201. public event ReloadGridStyleEventHandle ReloadGridStyleEvent;
  202. /// <summary>
  203. /// Initializes a new instance of the <see cref="UCListView" /> class.
  204. /// </summary>
  205. public UCListView()
  206. {
  207. InitializeComponent();
  208. }
  209. /// <summary>
  210. /// ms the page show source changed.
  211. /// </summary>
  212. /// <param name="currentSource">The current source.</param>
  213. void m_page_ShowSourceChanged(object currentSource)
  214. {
  215. this.DataSource = currentSource;
  216. }
  217. #region 重新加载数据源
  218. /// <summary>
  219. /// 功能描述:重新加载数据源
  220. /// 作  者:HZH
  221. /// 创建日期:2019-06-27 16:47:32
  222. /// 任务编号:POS
  223. /// </summary>
  224. public void ReloadSource()
  225. {
  226. try
  227. {
  228. if (DesignMode)
  229. return;
  230. ControlHelper.FreezeControl(this, true);
  231. if (this.panMain.Controls.Count <= 0)
  232. {
  233. ReloadGridStyle();
  234. }
  235. if (m_dataSource == null || ((IList)m_dataSource).Count <= 0)
  236. {
  237. for (int i = this.panMain.Controls.Count - 1; i >= 0; i--)
  238. {
  239. this.panMain.Controls[i].Visible = false;
  240. }
  241. return;
  242. }
  243. int intCount = Math.Min(((IList)m_dataSource).Count, this.panMain.Controls.Count);
  244. for (int i = 0; i < intCount; i++)
  245. {
  246. ((IListViewItem)this.panMain.Controls[i]).DataSource = ((IList)m_dataSource)[i];
  247. if (m_selectedSource.Contains(((IList)m_dataSource)[i]))
  248. {
  249. ((IListViewItem)this.panMain.Controls[i]).SetSelected(true);
  250. }
  251. else
  252. {
  253. ((IListViewItem)this.panMain.Controls[i]).SetSelected(false);
  254. }
  255. this.panMain.Controls[i].Visible = true;
  256. }
  257. for (int i = this.panMain.Controls.Count - 1; i >= intCount; i--)
  258. {
  259. if (this.panMain.Controls[i].Visible)
  260. this.panMain.Controls[i].Visible = false;
  261. }
  262. }
  263. finally
  264. {
  265. ControlHelper.FreezeControl(this, false);
  266. }
  267. }
  268. #endregion
  269. #region 刷新表格
  270. /// <summary>
  271. /// 功能描述:刷新表格样式
  272. /// 作  者:HZH
  273. /// 创建日期:2019-06-27 16:35:25
  274. /// 任务编号:POS
  275. /// </summary>
  276. public void ReloadGridStyle()
  277. {
  278. if (DesignMode)
  279. return;
  280. Form frmMain = this.FindForm();
  281. if (frmMain != null && !frmMain.IsDisposed && frmMain.Visible && this.Visible)
  282. {
  283. GetCellCount();
  284. try
  285. {
  286. ControlHelper.FreezeControl(this, true);
  287. if (this.panMain.Controls.Count < m_intCellCount)
  288. {
  289. int intControlsCount = this.panMain.Controls.Count;
  290. for (int i = 0; i < m_intCellCount - intControlsCount; i++)
  291. {
  292. Control uc = (Control)Activator.CreateInstance(m_itemType);
  293. uc.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5);
  294. (uc as IListViewItem).SelectedItemEvent += UCListView_SelectedItemEvent;
  295. uc.Visible = false;
  296. this.panMain.Controls.Add(uc);
  297. }
  298. }
  299. else if (this.panMain.Controls.Count > m_intCellCount)
  300. {
  301. int intControlsCount = this.panMain.Controls.Count;
  302. for (int i = intControlsCount - 1; i > m_intCellCount - 1; i--)
  303. {
  304. this.panMain.Controls.RemoveAt(i);
  305. }
  306. }
  307. foreach (Control item in this.panMain.Controls)
  308. {
  309. item.Size = new Size(m_intCellWidth, m_intCellHeight);
  310. }
  311. }
  312. finally
  313. {
  314. ControlHelper.FreezeControl(this, false);
  315. }
  316. if (ReloadGridStyleEvent != null)
  317. {
  318. ReloadGridStyleEvent(m_intCellCount);
  319. }
  320. }
  321. }
  322. /// <summary>
  323. /// Handles the SelectedItemEvent event of the UCListView control.
  324. /// </summary>
  325. /// <param name="sender">The source of the event.</param>
  326. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  327. void UCListView_SelectedItemEvent(object sender, EventArgs e)
  328. {
  329. var selectedItem = sender as IListViewItem;
  330. if (m_selectedSource.Contains(selectedItem.DataSource))
  331. {
  332. m_selectedSource.Remove(selectedItem.DataSource);
  333. selectedItem.SetSelected(false);
  334. }
  335. else
  336. {
  337. if (m_isMultiple)
  338. {
  339. m_selectedSource.Add(selectedItem.DataSource);
  340. selectedItem.SetSelected(true);
  341. }
  342. else
  343. {
  344. if (m_selectedSource.Count > 0)
  345. {
  346. int intCount = Math.Min(((IList)m_dataSource).Count, this.panMain.Controls.Count);
  347. for (int i = 0; i < intCount; i++)
  348. {
  349. var item = ((IListViewItem)this.panMain.Controls[i]);
  350. if (m_selectedSource.Contains(item.DataSource))
  351. {
  352. item.SetSelected(false);
  353. break;
  354. }
  355. }
  356. }
  357. m_selectedSource = new List<object>() { selectedItem.DataSource };
  358. selectedItem.SetSelected(true);
  359. }
  360. }
  361. if (SelectedItemEvent != null)
  362. {
  363. SelectedItemEvent(sender, e);
  364. }
  365. }
  366. #endregion
  367. #region 获取cell总数
  368. /// <summary>
  369. /// 功能描述:获取cell总数
  370. /// 作  者:HZH
  371. /// 创建日期:2019-06-27 16:28:58
  372. /// 任务编号:POS
  373. /// </summary>
  374. private void GetCellCount()
  375. {
  376. if (DesignMode)
  377. return;
  378. if (this.panMain.Width == 0)
  379. return;
  380. Control item = (Control)Activator.CreateInstance(m_itemType);
  381. int intXCount = (this.panMain.Width - 10) / (item.Width + 10);
  382. m_intCellWidth = item.Width + ((this.panMain.Width - 10) % (item.Width + 10)) / intXCount;
  383. int intYCount = (this.panMain.Height - 10) / (item.Height + 10);
  384. m_intCellHeight = item.Height + ((this.panMain.Height - 10) % (item.Height + 10)) / intYCount;
  385. int intCount = intXCount * intYCount;
  386. if (Page == null)
  387. {
  388. if (m_dataSource == null)
  389. {
  390. intCount = 0;
  391. }
  392. else
  393. {
  394. if (((IList)m_dataSource).Count > intCount)
  395. {
  396. intXCount = (this.panMain.Width - 10 - 20) / (item.Width + 10);
  397. m_intCellWidth = item.Width + ((this.panMain.Width - 10 - 20) % (item.Width + 10)) / intXCount;
  398. }
  399. intCount = Math.Max(intCount, ((IList)m_dataSource).Count);
  400. }
  401. }
  402. CellCount = intCount;
  403. }
  404. #endregion
  405. /// <summary>
  406. /// Handles the Resize event of the panMain control.
  407. /// </summary>
  408. /// <param name="sender">The source of the event.</param>
  409. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  410. private void panMain_Resize(object sender, EventArgs e)
  411. {
  412. ReloadGridStyle();
  413. }
  414. /// <summary>
  415. /// Handles the Load event of the UCListView control.
  416. /// </summary>
  417. /// <param name="sender">The source of the event.</param>
  418. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  419. private void UCListView_Load(object sender, EventArgs e)
  420. {
  421. }
  422. }
  423. }