UCMenuParentItem.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-15-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCMenuParentItem.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. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// 父类节点
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// Implements the <see cref="HZH_Controls.Controls.IMenuItem" />
  30. /// </summary>
  31. /// <seealso cref="System.Windows.Forms.UserControl" />
  32. /// <seealso cref="HZH_Controls.Controls.IMenuItem" />
  33. [ToolboxItem(false)]
  34. public partial class UCMenuParentItem : UserControl, IMenuItem
  35. {
  36. /// <summary>
  37. /// Occurs when [selected item].
  38. /// </summary>
  39. public event EventHandler SelectedItem;
  40. /// <summary>
  41. /// The m data source
  42. /// </summary>
  43. private MenuItemEntity m_dataSource;
  44. /// <summary>
  45. /// Gets or sets the data source.
  46. /// </summary>
  47. /// <value>The data source.</value>
  48. public MenuItemEntity DataSource
  49. {
  50. get
  51. {
  52. return m_dataSource;
  53. }
  54. set
  55. {
  56. m_dataSource = value;
  57. if (value != null)
  58. {
  59. lblTitle.Text = value.Text;
  60. }
  61. }
  62. }
  63. /// <summary>
  64. /// Initializes a new instance of the <see cref="UCMenuParentItem" /> class.
  65. /// </summary>
  66. public UCMenuParentItem()
  67. {
  68. InitializeComponent();
  69. lblTitle.MouseDown += lblTitle_MouseDown;
  70. }
  71. /// <summary>
  72. /// 设置样式
  73. /// </summary>
  74. /// <param name="styles">key:属性名称,value:属性值</param>
  75. /// <exception cref="System.Exception">菜单元素设置样式异常</exception>
  76. /// <exception cref="Exception">菜单元素设置样式异常</exception>
  77. public void SetStyle(Dictionary<string, object> styles)
  78. {
  79. Type t = this.GetType();
  80. foreach (var item in styles)
  81. {
  82. var pro = t.GetProperty(item.Key);
  83. if (pro != null && pro.CanWrite)
  84. {
  85. try
  86. {
  87. pro.SetValue(this, item.Value, null);
  88. }
  89. catch (Exception ex)
  90. {
  91. throw new Exception("菜单元素设置样式异常", ex);
  92. }
  93. }
  94. }
  95. }
  96. /// <summary>
  97. /// 设置选中样式
  98. /// </summary>
  99. /// <param name="blnSelected">是否选中</param>
  100. public void SetSelectedStyle(bool? blnSelected)
  101. {
  102. if (blnSelected.HasValue)
  103. {
  104. if (blnSelected.Value)
  105. {
  106. this.lblTitle.Image = Properties.Resources.sanjiao1;
  107. }
  108. else
  109. {
  110. this.lblTitle.Image = Properties.Resources.sanjiao2;
  111. }
  112. }
  113. else
  114. {
  115. this.lblTitle.Image = null;
  116. }
  117. }
  118. /// <summary>
  119. /// Handles the MouseDown event of the lblTitle control.
  120. /// </summary>
  121. /// <param name="sender">The source of the event.</param>
  122. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  123. void lblTitle_MouseDown(object sender, MouseEventArgs e)
  124. {
  125. if (SelectedItem != null)
  126. {
  127. SelectedItem(this, e);
  128. }
  129. }
  130. }
  131. }