MultiSelectComboBox.xaml.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Collections.ObjectModel;
  15. using System.ComponentModel;
  16. namespace GDNXFD.Alert.Config.Controls
  17. {
  18. /// <summary>
  19. /// Interaction logic for MultiSelectComboBox.xaml
  20. /// </summary>
  21. public partial class MultiSelectComboBox : UserControl
  22. {
  23. private ObservableCollection<Node> _nodeList;
  24. public MultiSelectComboBox()
  25. {
  26. InitializeComponent();
  27. _nodeList = new ObservableCollection<Node>();
  28. }
  29. #region Dependency Properties
  30. public static readonly DependencyProperty ItemsSourceProperty =
  31. DependencyProperty.Register("ItemsSource", typeof(Dictionary<string, object>), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(null,
  32. new PropertyChangedCallback(MultiSelectComboBox.OnItemsSourceChanged)));
  33. public static readonly DependencyProperty SelectedItemsProperty =
  34. DependencyProperty.Register("SelectedItems", typeof(Dictionary<string, object>), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(null,
  35. new PropertyChangedCallback(MultiSelectComboBox.OnSelectedItemsChanged)));
  36. // public static readonly DependencyProperty SelectedItemsProperty =
  37. //DependencyProperty.Register("SelectedItems", typeof(Dictionary<string, object>), typeof(MultiSelectComboBox), new UIPropertyMetadata(null));
  38. public static readonly DependencyProperty TextProperty =
  39. DependencyProperty.Register("Text", typeof(string), typeof(MultiSelectComboBox), new UIPropertyMetadata(string.Empty));
  40. public static readonly DependencyProperty DefaultTextProperty =
  41. DependencyProperty.Register("DefaultText", typeof(string), typeof(MultiSelectComboBox), new UIPropertyMetadata(string.Empty));
  42. public Dictionary<string, object> ItemsSource
  43. {
  44. get { return (Dictionary<string, object>)GetValue(ItemsSourceProperty); }
  45. set
  46. {
  47. SetValue(ItemsSourceProperty, value);
  48. }
  49. }
  50. public Dictionary<string, object> SelectedItems
  51. {
  52. get { return (Dictionary<string, object>)GetValue(SelectedItemsProperty); }
  53. set
  54. {
  55. SetValue(SelectedItemsProperty, value);
  56. }
  57. }
  58. public string Text
  59. {
  60. get { return (string)GetValue(TextProperty); }
  61. set { SetValue(TextProperty, value); }
  62. }
  63. public string DefaultText
  64. {
  65. get { return (string)GetValue(DefaultTextProperty); }
  66. set { SetValue(DefaultTextProperty, value); }
  67. }
  68. #endregion
  69. #region Events
  70. private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  71. {
  72. MultiSelectComboBox control = (MultiSelectComboBox)d;
  73. control.DisplayInControl();
  74. }
  75. private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  76. {
  77. MultiSelectComboBox control = (MultiSelectComboBox)d;
  78. control.SelectNodes();
  79. control.SetText();
  80. }
  81. private void CheckBox_Click(object sender, RoutedEventArgs e)
  82. {
  83. var xxx = SelectedItems;
  84. CheckBox clickedBox = (CheckBox)sender;
  85. if (clickedBox.Content == "全部" )
  86. {
  87. if (clickedBox.IsChecked.Value)
  88. {
  89. foreach (Node node in _nodeList)
  90. {
  91. node.IsSelected = true;
  92. }
  93. }
  94. else
  95. {
  96. foreach (Node node in _nodeList)
  97. {
  98. node.IsSelected = false;
  99. }
  100. }
  101. }
  102. else
  103. {
  104. int _selectedCount = 0;
  105. foreach (Node s in _nodeList)
  106. {
  107. if (s.IsSelected && s.Title != "全部")
  108. _selectedCount++;
  109. }
  110. if (_selectedCount == _nodeList.Count - 1)
  111. _nodeList.FirstOrDefault(i => i.Title == "全部").IsSelected = true;
  112. else
  113. _nodeList.FirstOrDefault(i => i.Title == "全部").IsSelected = false;
  114. }
  115. SetSelectedItems();
  116. SetText();
  117. if (SelectionChanged != null)
  118. {
  119. SelectionChanged(this, e);
  120. }
  121. }
  122. #endregion
  123. #region Methods
  124. private void SelectNodes()
  125. {
  126. foreach (KeyValuePair<string, object> keyValue in SelectedItems)
  127. {
  128. Node node = _nodeList.FirstOrDefault(i => i.Title == keyValue.Key);
  129. if (node != null)
  130. node.IsSelected = true;
  131. }
  132. if (SelectedItems != null && SelectedItems.Count == ItemsSource.Count)
  133. {
  134. Node node = _nodeList.FirstOrDefault(i => i.Title == "全部");
  135. if (node != null)
  136. node.IsSelected = true;
  137. }
  138. }
  139. private void SetSelectedItems()
  140. {
  141. if (SelectedItems == null)
  142. SelectedItems = new Dictionary<string, object>();
  143. SelectedItems.Clear();
  144. foreach (Node node in _nodeList)
  145. {
  146. if (node.IsSelected && node.Title != "全部")
  147. {
  148. if (this.ItemsSource.Count > 0)
  149. SelectedItems.Add(node.Title, this.ItemsSource[node.Title]);
  150. }
  151. }
  152. }
  153. private void DisplayInControl()
  154. {
  155. _nodeList.Clear();
  156. if (this.ItemsSource.Count > 0)
  157. _nodeList.Add(new Node("全部"));
  158. foreach (KeyValuePair<string, object> keyValue in this.ItemsSource)
  159. {
  160. Node node = new Node(keyValue.Key);
  161. _nodeList.Add(node);
  162. }
  163. MultiSelectCombo.ItemsSource = _nodeList;
  164. }
  165. private void SetText()
  166. {
  167. if (this.SelectedItems != null)
  168. {
  169. StringBuilder displayText = new StringBuilder();
  170. foreach (Node s in _nodeList)
  171. {
  172. if (s.IsSelected == true && s.Title == "全部")
  173. {
  174. displayText = new StringBuilder();
  175. displayText.Append("全部");
  176. break;
  177. }
  178. else if (s.IsSelected == true && s.Title != "全部")
  179. {
  180. displayText.Append(s.Title);
  181. displayText.Append(',');
  182. }
  183. }
  184. this.Text = displayText.ToString().TrimEnd(new char[] { ',' });
  185. }
  186. // set DefaultText if nothing else selected
  187. if (string.IsNullOrEmpty(this.Text))
  188. {
  189. this.Text = this.DefaultText;
  190. }
  191. this.ToolTip = this.Text;
  192. }
  193. #endregion
  194. #region 自定义事件
  195. public event EventHandler SelectionChanged;
  196. #endregion
  197. }
  198. public class Node : INotifyPropertyChanged
  199. {
  200. private string _title;
  201. private bool _isSelected;
  202. #region ctor
  203. public Node(string title)
  204. {
  205. Title = title;
  206. }
  207. #endregion
  208. #region Properties
  209. public string Title
  210. {
  211. get
  212. {
  213. return _title;
  214. }
  215. set
  216. {
  217. _title = value;
  218. NotifyPropertyChanged("Title");
  219. }
  220. }
  221. public bool IsSelected
  222. {
  223. get
  224. {
  225. return _isSelected;
  226. }
  227. set
  228. {
  229. _isSelected = value;
  230. NotifyPropertyChanged("IsSelected");
  231. }
  232. }
  233. #endregion
  234. public event PropertyChangedEventHandler PropertyChanged;
  235. protected void NotifyPropertyChanged(string propertyName)
  236. {
  237. if (PropertyChanged != null)
  238. {
  239. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  240. }
  241. }
  242. }
  243. }