CustomComboControl.xaml.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace PDF_Office.CustomControl.CompositeControl
  16. {
  17. //用法:
  18. // IsValueContent为ture时,使用ValueStr作为集合某项的值
  19. //IsValueContent为false时,使用Value作为集合某项的值
  20. //SetItemSource(List<ComboDataItem> items)更换集合List
  21. //默认:字体集合、IsValueContent为false、Value作为集合某项的值
  22. public class ComboDataItem
  23. {
  24. public string ValueStr { get; private set; }
  25. public double Value { get; private set; }
  26. public string Content { get; private set; }
  27. public string Unit { get; private set; }
  28. public ComboDataItem(double value, string unitStr = "")
  29. {
  30. Content = value + unitStr;
  31. Value = value;
  32. }
  33. public ComboDataItem(string valueStr, string contentStr = "")
  34. {
  35. Content = contentStr;
  36. ValueStr = valueStr;
  37. }
  38. }
  39. /// <summary>
  40. /// CustomComboControl.xaml 的交互逻辑
  41. /// </summary>
  42. public partial class CustomComboControl : UserControl
  43. {
  44. public List<ComboDataItem> Items { get; private set; }
  45. public event RoutedEventHandler ValueChanged;
  46. public CustomComboControl()
  47. {
  48. InitializeComponent();
  49. Items = new List<ComboDataItem>();
  50. DefaultItems();
  51. }
  52. //更换集合
  53. public void SetItemSource(List<ComboDataItem> items)
  54. {
  55. SelectedIndex = - 1;//为了触发SelectedIndex
  56. Items = items;
  57. comBox.ItemsSource = Items;
  58. SelectedIndex = ((Items == null || Items.Count == 0) ? -1 : 0);
  59. }
  60. /// <summary>
  61. /// 默认集合(字体集合)
  62. /// </summary>
  63. private void DefaultItems()
  64. {
  65. Items.Clear();
  66. SelectedIndex = -1;
  67. var item = new ComboDataItem(3);
  68. Items.Add(item);
  69. item = new ComboDataItem(6);
  70. Items.Add(item);
  71. item = new ComboDataItem(9);
  72. Items.Add(item);
  73. item = new ComboDataItem(12);
  74. Items.Add(item);
  75. item = new ComboDataItem(15);
  76. Items.Add(item);
  77. item = new ComboDataItem(18);
  78. Items.Add(item);
  79. item = new ComboDataItem(21);
  80. Items.Add(item);
  81. item = new ComboDataItem(24);
  82. Items.Add(item);
  83. comBox.ItemsSource = Items;
  84. SelectedIndex = ((Items == null || Items.Count == 0) ? -1 : 0);
  85. }
  86. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  87. {
  88. comBox.SelectionChanged -= comBox_SelectionChanged;
  89. comBox.SelectionChanged += comBox_SelectionChanged;
  90. }
  91. private void comBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  92. {
  93. var item = comBox.SelectedItem as ComboDataItem;
  94. if (item != null)
  95. {
  96. Value = item.Value;
  97. ValueStr = item.ValueStr;
  98. title.Text = item.Content;
  99. if (IsValueContent == false)
  100. ValueChanged?.Invoke(Value, e);
  101. else
  102. ValueChanged?.Invoke(ValueStr, e);
  103. }
  104. }
  105. public double Value
  106. {
  107. get { return (double)GetValue(ValueProperty); }
  108. set { SetValue(ValueProperty, value); }
  109. }
  110. public string ValueStr
  111. {
  112. get { return (string)GetValue(ValueStrProperty); }
  113. set { SetValue(ValueStrProperty, value); }
  114. }
  115. public bool IsValueContent
  116. {
  117. get { return (bool)GetValue(IsValueContentProperty); }
  118. set { SetValue(IsValueContentProperty, value); }
  119. }
  120. public List<ComboDataItem> ItemSource
  121. {
  122. get { return (List<ComboDataItem>)GetValue(ItemSourceProperty); }
  123. set { SetValue(ItemSourceProperty, value); }
  124. }
  125. public int SelectedIndex
  126. {
  127. get { return (int)GetValue(SelectedIndexProperty); }
  128. set { SetValue(SelectedIndexProperty, value); }
  129. }
  130. public static readonly DependencyProperty ValueProperty =
  131. DependencyProperty.Register("Value", typeof(double), typeof(CustomComboControl), new PropertyMetadata(1.0, SelectedValuePropertyChanged));
  132. public static readonly DependencyProperty ValueStrProperty =
  133. DependencyProperty.Register("ValueStr", typeof(string), typeof(CustomComboControl), new PropertyMetadata("", SelectedValueStrPropertyChanged));
  134. public static readonly DependencyProperty ItemSourceProperty =
  135. DependencyProperty.Register("ItemSource", typeof(List<ComboDataItem>), typeof(CustomComboControl), new PropertyMetadata(null, SelectedItemSourcePropertyChanged));
  136. public static readonly DependencyProperty SelectedIndexProperty =
  137. DependencyProperty.Register("SelectedIndex", typeof(int), typeof(CustomComboControl), new PropertyMetadata(-1, SelectedIndexPropertyChanged));
  138. public static readonly DependencyProperty IsValueContentProperty =
  139. DependencyProperty.Register("IsValueContent", typeof(bool), typeof(CustomComboControl), new PropertyMetadata(false));
  140. private static void SelectedValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  141. {
  142. var control = d as CustomComboControl;
  143. var value = (double)e.NewValue;
  144. if (control != null && control.IsValueContent == false)
  145. {
  146. control.ValueChanged?.Invoke(value, null);
  147. }
  148. }
  149. private static void SelectedValueStrPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  150. {
  151. var control = d as CustomComboControl;
  152. var value = (string)e.NewValue;
  153. if (control != null && control.IsValueContent)
  154. {
  155. control.ValueChanged?.Invoke(value, null);
  156. }
  157. }
  158. private static void SelectedItemSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  159. {
  160. var control = d as CustomComboControl;
  161. var value = (List<ComboDataItem>)e.NewValue;
  162. if (control != null)
  163. {
  164. control.SelectedIndex = -1;
  165. control.comBox.ItemsSource = value;
  166. control.Items = value;
  167. control.SelectedIndex = ((value == null || value.Count == 0) ? -1 : 0);
  168. }
  169. }
  170. private static void SelectedIndexPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  171. {
  172. var control = d as CustomComboControl;
  173. var value = (int)e.NewValue;
  174. if (control != null)
  175. {
  176. if(control.comBox.Items != null && control.comBox.Items.Count > 0)
  177. {
  178. control.comBox.SelectedIndex = value;
  179. }
  180. control.UpdateSelectedIndex();
  181. }
  182. }
  183. public void UpdateSelectedIndex()
  184. {
  185. if(SelectedIndex < 0 || comBox.Items == null || comBox.Items.Count <= SelectedIndex)
  186. {
  187. title.Text = "";
  188. }
  189. else
  190. {
  191. title.Text = Items[SelectedIndex].Content;
  192. }
  193. }
  194. }
  195. }