Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ComboBoxHelper.cs 7.3 KiB

1 ano atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using BPA.UIControl.Commons.KnownBoxes;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Collections.Specialized;
  6. using System.ComponentModel;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. namespace BPA.UIControl
  11. {
  12. /// <summary>
  13. /// ComboBox 帮助类
  14. /// </summary>
  15. public class ComboBoxHelper
  16. {
  17. /// <summary>
  18. /// 是否多选
  19. /// </summary>
  20. public static readonly DependencyProperty IsMultiSelectProperty = DependencyProperty.RegisterAttached(
  21. "IsMultiSelect", typeof(bool), typeof(ComboBoxHelper), new PropertyMetadata(BooleanBoxes.FalseBox, OnIsMultiSelectChanged));
  22. public static void SetIsMultiSelect(DependencyObject element, bool value)
  23. {
  24. element.SetValue(IsMultiSelectProperty, BooleanBoxes.Box(value));
  25. }
  26. public static bool GetIsMultiSelect(DependencyObject element)
  27. {
  28. return (bool)element.GetValue(IsMultiSelectProperty);
  29. }
  30. /// <summary>
  31. /// 子项是否选中
  32. /// </summary>
  33. internal static readonly DependencyProperty IsSelectedProperty = DependencyProperty.RegisterAttached(
  34. "IsSelected", typeof(bool), typeof(ComboBoxHelper), new PropertyMetadata(BooleanBoxes.FalseBox, OnIsSelectedChanged));
  35. internal static void SetIsSelected(DependencyObject element, bool value)
  36. {
  37. element.SetValue(IsSelectedProperty, BooleanBoxes.Box(value));
  38. }
  39. internal static bool GetIsSelected(DependencyObject element)
  40. {
  41. return (bool)element.GetValue(IsSelectedProperty);
  42. }
  43. /// <summary>
  44. /// 多选时显示文本
  45. /// </summary>
  46. public static readonly DependencyProperty MultiSelectTextProperty = DependencyProperty.RegisterAttached(
  47. "MultiSelectText", typeof(string), typeof(ComboBoxHelper), new PropertyMetadata(null));
  48. public static void SetMultiSelectText(DependencyObject element, string value)
  49. {
  50. element.SetValue(MultiSelectTextProperty, value);
  51. }
  52. public static string GetMultiSelectText(DependencyObject element)
  53. {
  54. return (string)element.GetValue(MultiSelectTextProperty);
  55. }
  56. /// <summary>
  57. /// 多选时文本分隔符
  58. /// </summary>
  59. public static readonly DependencyProperty MultiSelectSeparatorProperty = DependencyProperty.RegisterAttached(
  60. "MultiSelectSeparator", typeof(string), typeof(ComboBoxHelper), new PropertyMetadata(", ", OnMultiSelectSeparatorChanged));
  61. public static void SetMultiSelectSeparator(DependencyObject element, string value)
  62. {
  63. element.SetValue(MultiSelectSeparatorProperty, value);
  64. }
  65. public static string GetMultiSelectSeparator(DependencyObject element)
  66. {
  67. return (string)element.GetValue(MultiSelectSeparatorProperty);
  68. }
  69. /// <summary>
  70. /// 选中项集合
  71. /// </summary>
  72. public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.RegisterAttached(
  73. "SelectedItems", typeof(IList), typeof(ComboBoxHelper), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedItemsChanged));
  74. public static void SetSelectedItems(DependencyObject element, IList value)
  75. {
  76. element.SetValue(SelectedItemsProperty, value);
  77. }
  78. public static IList GetSelectedItems(DependencyObject element)
  79. {
  80. return (IList)element.GetValue(SelectedItemsProperty);
  81. }
  82. private static void OnIsMultiSelectChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  83. {
  84. var comboBox = d as ComboBox;
  85. if ((bool)e.NewValue)
  86. {
  87. comboBox.DropDownOpened += ComboBox_DropDownOpened;
  88. }
  89. else
  90. {
  91. comboBox.DropDownOpened -= ComboBox_DropDownOpened;
  92. }
  93. }
  94. private static async void ComboBox_DropDownOpened(object sender, System.EventArgs e)
  95. {
  96. await Task.Delay(100);
  97. var comboBox = sender as ComboBox;
  98. var selectedItems = GetSelectedItems(comboBox);
  99. foreach (var item in comboBox.Items)
  100. {
  101. if (comboBox.ItemContainerGenerator.ContainerFromItem(item) is not ComboBoxItem comboBoxItem)
  102. {
  103. continue;
  104. }
  105. if (selectedItems.Contains(item))
  106. {
  107. SetIsSelected(comboBoxItem, true);
  108. }
  109. else
  110. {
  111. SetIsSelected(comboBoxItem, false);
  112. }
  113. }
  114. }
  115. private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  116. {
  117. var comboBox = d as ComboBox;
  118. void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
  119. {
  120. UpdateMultiSelectText(comboBox);
  121. }
  122. if (e.NewValue is INotifyCollectionChanged newCollection)
  123. {
  124. newCollection.CollectionChanged += OnCollectionChanged;
  125. }
  126. UpdateMultiSelectText(comboBox);
  127. }
  128. private static void UpdateMultiSelectText(ComboBox comboBox)
  129. {
  130. var selectedItems = GetSelectedItems(comboBox);
  131. if (selectedItems is null)
  132. {
  133. SetMultiSelectText(comboBox, string.Empty);
  134. return;
  135. }
  136. var texts = new List<string>();
  137. foreach (var item in selectedItems)
  138. {
  139. var typeConverter = TypeDescriptor.GetConverter(item.GetType());
  140. var text = typeConverter.ConvertToString(item);
  141. texts.Add(text);
  142. }
  143. var separator = GetMultiSelectSeparator(comboBox);
  144. SetMultiSelectText(comboBox, string.Join(separator, texts));
  145. }
  146. private static void OnMultiSelectSeparatorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  147. {
  148. UpdateMultiSelectText(d as ComboBox);
  149. }
  150. private static void OnIsSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  151. {
  152. var comboBoxItem = d as ComboBoxItem;
  153. var comboBox = ItemsControl.ItemsControlFromItemContainer(d) as ComboBox;
  154. var index = comboBox.ItemContainerGenerator.IndexFromContainer(comboBoxItem);
  155. var itemData = comboBox.Items[index];
  156. var selectedItems = GetSelectedItems(comboBox);
  157. if (selectedItems == null)
  158. {
  159. selectedItems = new ObservableCollection<object>();
  160. SetSelectedItems(comboBox, selectedItems);
  161. }
  162. if ((bool)e.NewValue)
  163. {
  164. if (!selectedItems.Contains(itemData))
  165. {
  166. selectedItems.Add(itemData); // 添加
  167. }
  168. }
  169. else
  170. {
  171. if (selectedItems.Contains(itemData))
  172. {
  173. selectedItems.Remove(itemData); // 移除
  174. }
  175. }
  176. // UpdateMultiSelectText(comboBox);
  177. }
  178. }
  179. }