NativeCombox.xaml.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Shapes;
  15. namespace CollaborativePlatformMain.Form.UserControlForm
  16. {
  17. /// <summary>
  18. /// NativeCombox.xaml 的交互逻辑
  19. /// </summary>
  20. public partial class NativeCombox : UserControl
  21. {
  22. /// <summary>
  23. /// 初始化渲染
  24. /// </summary>
  25. public NativeCombox()
  26. {
  27. InitializeComponent();
  28. }
  29. /// <summary>
  30. /// 事件
  31. /// </summary>
  32. /// <param name="sender"></param>
  33. /// <param name="e"></param>
  34. public delegate void comboxHeand(object sender, EventArgs e);
  35. /// <summary>
  36. /// checkboc按钮点击命令
  37. /// </summary>
  38. public event comboxHeand UserControlComBoxClick;
  39. /// <summary>
  40. /// 下拉按钮点击命令
  41. /// </summary>
  42. public event comboxHeand UserControlToggleClick;
  43. /// <summary>
  44. /// 当前 NativeCombox 选中的值
  45. /// </summary>
  46. public List<string> listKey;
  47. /// <summary>
  48. /// ItemsSource属性
  49. /// </summary>
  50. public List<NativeDataInfo> ItemsSource
  51. {
  52. get { return (List<NativeDataInfo>)GetValue(ItemsSourceProperty); }
  53. set
  54. {
  55. SetValue(ItemsSourceProperty, value);
  56. SetText();
  57. }
  58. }
  59. /// <summary>
  60. /// ItemsSource实体属性
  61. /// </summary>
  62. public static readonly DependencyProperty ItemsSourceProperty =
  63. DependencyProperty.Register("ItemsSource", typeof(object), typeof(NativeCombox), new FrameworkPropertyMetadata(null, ItemsSourcePropertyChangedCallback));
  64. /// <summary>
  65. /// ItemsSource实体属性改变事件
  66. /// </summary>
  67. /// <param name="dependencyObject"></param>
  68. /// <param name="e"></param>
  69. private static void ItemsSourcePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
  70. {
  71. var multioleCheckbox = dependencyObject as NativeCombox;
  72. if (multioleCheckbox == null) return;
  73. multioleCheckbox.CheckableCombo.ItemsSource = multioleCheckbox.ItemsSource;
  74. }
  75. /// <summary>
  76. /// Text属性
  77. /// </summary>
  78. public string Text
  79. {
  80. get { return (string)GetValue(TextProperty); }
  81. set { SetValue(TextProperty, value); }
  82. }
  83. /// <summary>
  84. /// text标题实体
  85. /// </summary>
  86. public static readonly DependencyProperty TextProperty =
  87. DependencyProperty.Register("Text", typeof(string), typeof(NativeCombox), new FrameworkPropertyMetadata("", TextPropertyChangedCallback));
  88. private static void TextPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
  89. {
  90. var multioleCheckbox = dependencyObject as NativeCombox;
  91. if (multioleCheckbox == null) return;
  92. }
  93. /// <summary>
  94. /// 默认标题
  95. /// </summary>
  96. public string DefaultText
  97. {
  98. get { return (string)GetValue(DefaultTextProperty); }
  99. set { SetValue(DefaultTextProperty, value); }
  100. }
  101. /// <summary>
  102. /// 默认标题实体
  103. /// </summary>
  104. public static readonly DependencyProperty DefaultTextProperty =
  105. DependencyProperty.RegisterAttached("DefaultText", typeof(string), typeof(NativeCombox), new UIPropertyMetadata(string.Empty));
  106. /// <summary>
  107. /// checkBox 选中事件
  108. /// </summary>
  109. /// <param name="sender"></param>
  110. /// <param name="e"></param>
  111. private void CheckBox_Click(object sender, RoutedEventArgs e)
  112. {
  113. var checkbox = sender as CheckBox;
  114. if (checkbox == null) return;
  115. SetText();
  116. if (UserControlComBoxClick != null)
  117. UserControlComBoxClick(sender, new EventArgs());
  118. }
  119. /// <summary>
  120. /// 设置text值
  121. /// </summary>
  122. public void SetText()
  123. {
  124. Text = "";
  125. listKey = new List<string>();
  126. foreach (var item in ItemsSource)
  127. {
  128. if (item.IsSelect)
  129. {
  130. Text += item.Name + ",";
  131. listKey.Add(item.Key);
  132. }
  133. }
  134. Text = string.IsNullOrEmpty(Text) ? DefaultText : Text.TrimEnd(new[] { ',' });
  135. }
  136. /// <summary>
  137. /// 下拉按钮点击
  138. /// </summary>
  139. /// <param name="sender"></param>
  140. /// <param name="e"></param>
  141. private void ToggleButton_Click(object sender, RoutedEventArgs e)
  142. {
  143. if (UserControlToggleClick != null)
  144. UserControlToggleClick(sender, new EventArgs());
  145. }
  146. }
  147. /// <summary>
  148. ///
  149. /// <para>文件名(File Name): NativeDataInfo.cs</para>
  150. ///
  151. /// <para>描述(Description): combox控件实体数据源</para>
  152. ///
  153. /// <para>数据表(Tables): nothing</para>
  154. ///
  155. /// <para>作者(Author): Li Wen Jin</para>
  156. ///
  157. /// <para>日期(Create Date): 2023年3月4日09:37:20</para>
  158. ///
  159. /// 修改记录(Revision History):
  160. /// R1:
  161. /// 修改作者:
  162. /// 修改日期:
  163. /// 修改理由:
  164. ///
  165. /// </summary>
  166. public class NativeDataInfo : INotifyPropertyChanged
  167. {
  168. /// <summary>
  169. /// 唯一标识
  170. /// </summary>
  171. private string key;
  172. /// <summary>
  173. /// 唯一标识
  174. /// </summary>
  175. public string Key
  176. {
  177. get { return key; }
  178. set { key = value; }
  179. }
  180. /// <summary>
  181. /// 展示名称
  182. /// </summary>
  183. private string name;
  184. /// <summary>
  185. /// 展示名称
  186. /// </summary>
  187. public string Name
  188. {
  189. get { return name; }
  190. set { name = value; }
  191. }
  192. /// <summary>
  193. /// 是否选中
  194. /// </summary>
  195. private bool isSelect;
  196. /// <summary>
  197. /// 是否选中
  198. /// </summary>
  199. public bool IsSelect
  200. {
  201. get { return isSelect; }
  202. set
  203. {
  204. if (value != isSelect)
  205. {
  206. isSelect = value;
  207. NotifyPropertyChanged("IsSelect");
  208. }
  209. }
  210. }
  211. /// <summary>
  212. /// 数据绑定
  213. /// </summary>
  214. /// <param name="key">唯一标识</param>
  215. /// <param name="name">界面展示内容</param>
  216. public NativeDataInfo(string key, string name)
  217. {
  218. this.key = key;
  219. this.name = name;
  220. }
  221. public NativeDataInfo()
  222. {
  223. }
  224. /// <summary>
  225. /// 数据绑定
  226. /// </summary>
  227. /// <param name="isSelect"></param>
  228. /// <param name="name"></param>
  229. public NativeDataInfo(bool isSelect, string name)
  230. {
  231. this.name = name;
  232. this.isSelect = isSelect;
  233. }
  234. public event PropertyChangedEventHandler PropertyChanged;
  235. private void NotifyPropertyChanged(string propertyName = "")
  236. {
  237. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  238. }
  239. }
  240. }