MultipleSelectionsCombox.xaml.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using SciChart.Core.Extensions;
  18. namespace MECF.Framework.UI.Client.Ctrlib.Controls
  19. {
  20. public class MultipleCheckboxModel : INotifyPropertyChanged
  21. {
  22. public int Id { get; set; }
  23. public string Description { get; set; }
  24. private bool _isSelected;
  25. public bool IsSelected
  26. {
  27. get { return _isSelected; }
  28. set
  29. {
  30. _isSelected = value;
  31. NotifyPropertyChanged("IsSelected");
  32. }
  33. }
  34. #region INotifyPropertyChanged Members
  35. public event PropertyChangedEventHandler PropertyChanged;
  36. protected virtual void NotifyPropertyChanged(string propertyName)
  37. {
  38. if (this.PropertyChanged != null)
  39. {
  40. this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  41. }
  42. }
  43. #endregion
  44. }
  45. /// <summary>
  46. /// MultipleSelectionsCombox.xaml 的交互逻辑
  47. /// </summary>
  48. public partial class MultipleSelectionsCombox : UserControl
  49. {
  50. public MultipleSelectionsCombox()
  51. {
  52. InitializeComponent();
  53. }
  54. #region Dependency Properties
  55. public IEnumerable<MultipleCheckboxModel> ItemsSource
  56. {
  57. get { return (IEnumerable<MultipleCheckboxModel>)GetValue(ItemsSourceProperty); }
  58. set
  59. {
  60. SetValue(ItemsSourceProperty, value);
  61. SetText();
  62. }
  63. }
  64. public static readonly DependencyProperty ItemsSourceProperty =
  65. DependencyProperty.Register("ItemsSource", typeof(object), typeof(MultipleSelectionsCombox), new FrameworkPropertyMetadata(null, ItemsSourcePropertyChangedCallback));
  66. private static void ItemsSourcePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
  67. {
  68. var multioleCheckbox = dependencyObject as MultipleSelectionsCombox;
  69. if (multioleCheckbox == null) return;
  70. multioleCheckbox.CheckableCombo.ItemsSource = multioleCheckbox.ItemsSource;
  71. }
  72. public string Text
  73. {
  74. get { return (string)GetValue(TextProperty); }
  75. set { SetValue(TextProperty, value); }
  76. }
  77. public static readonly DependencyProperty TextProperty =
  78. DependencyProperty.Register("Text", typeof(string), typeof(MultipleSelectionsCombox), new FrameworkPropertyMetadata("", TextPropertyChangedCallback));
  79. private static void TextPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
  80. {
  81. var multioleCheckbox = dependencyObject as MultipleSelectionsCombox;
  82. if (multioleCheckbox == null) return;
  83. }
  84. public string DefaultText
  85. {
  86. get { return (string)GetValue(DefaultTextProperty); }
  87. set { SetValue(DefaultTextProperty, value); }
  88. }
  89. public static readonly DependencyProperty DefaultTextProperty =
  90. DependencyProperty.Register("DefaultText", typeof(string), typeof(MultipleSelectionsCombox), new UIPropertyMetadata(string.Empty));
  91. #endregion
  92. #region Event
  93. private void Checkbox_OnClick(object sender, RoutedEventArgs e)
  94. {
  95. var checkbox = sender as CheckBox;
  96. if (checkbox == null) return;
  97. if ((string)checkbox.Content == "All")
  98. {
  99. Text = "";
  100. if (checkbox.IsChecked != null && checkbox.IsChecked.Value)
  101. {
  102. ItemsSource.ForEachDo(x =>
  103. {
  104. x.IsSelected = true;
  105. Text = "All";
  106. });
  107. }
  108. else
  109. {
  110. ItemsSource.ForEachDo(x =>
  111. {
  112. x.IsSelected = false;
  113. Text = "None";
  114. });
  115. }
  116. }
  117. else
  118. {
  119. SetText();
  120. }
  121. }
  122. #endregion
  123. #region Private Method
  124. private void SetText()
  125. {
  126. Text = "";
  127. var all = ItemsSource.FirstOrDefault(x => x.Description == "All");
  128. foreach (var item in ItemsSource)
  129. {
  130. if (item.IsSelected && item.Description != "All")
  131. {
  132. Text += item.Description + ",";
  133. }
  134. else if (all != null)
  135. {
  136. if (all.IsSelected)
  137. all.IsSelected = false;
  138. }
  139. }
  140. Text = string.IsNullOrEmpty(Text) ? DefaultText : Text.TrimEnd(new[] { ',' });
  141. }
  142. #endregion
  143. }
  144. }