CustomComboControl.xaml.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. //字符串类型的值
  25. public string ValueStr { get; private set; }
  26. //数字类型的值
  27. public double Value { get; private set; }
  28. //下拉框显示的内容
  29. public string Content { get; private set; }
  30. //下拉框显示的内容+单位:限数字值的单位
  31. public string Unit { get; private set; }
  32. //数字类型
  33. public ComboDataItem(double value, string unitStr = "")
  34. {
  35. Content = value + unitStr;
  36. Value = value;
  37. }
  38. //字符串类型
  39. public ComboDataItem(string valueStr, string contentStr = "")
  40. {
  41. Content = contentStr;
  42. ValueStr = valueStr;
  43. }
  44. }
  45. /// <summary>
  46. /// CustomComboControl.xaml 的交互逻辑
  47. /// </summary>
  48. public partial class CustomComboControl : UserControl
  49. {
  50. public List<ComboDataItem> Items { get; private set; }
  51. public event RoutedEventHandler ValueChanged;
  52. public CustomComboControl()
  53. {
  54. InitializeComponent();
  55. Items = new List<ComboDataItem>();
  56. DefaultItems();
  57. }
  58. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  59. {
  60. comBox.SelectionChanged -= comBox_SelectionChanged;
  61. comBox.SelectionChanged += comBox_SelectionChanged;
  62. }
  63. //更换集合
  64. public void SetItemSource(List<ComboDataItem> items)
  65. {
  66. SelectedIndex = - 1;//为了触发SelectedIndex
  67. Items = items;
  68. ItemSource = Items;
  69. }
  70. /// <summary>
  71. /// 默认集合(字体集合)
  72. /// </summary>
  73. private void DefaultItems()
  74. {
  75. Items.Clear();
  76. SelectedIndex = -1;
  77. var item = new ComboDataItem(3);
  78. Items.Add(item);
  79. item = new ComboDataItem(6);
  80. Items.Add(item);
  81. item = new ComboDataItem(9);
  82. Items.Add(item);
  83. item = new ComboDataItem(12);
  84. Items.Add(item);
  85. item = new ComboDataItem(15);
  86. Items.Add(item);
  87. item = new ComboDataItem(18);
  88. Items.Add(item);
  89. item = new ComboDataItem(21);
  90. Items.Add(item);
  91. item = new ComboDataItem(24);
  92. Items.Add(item);
  93. ItemSource = Items;
  94. }
  95. //下拉框选中项
  96. private void comBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  97. {
  98. var item = comBox.SelectedItem as ComboDataItem;
  99. if (item != null)
  100. {
  101. title.Text = item.Content;
  102. SelectedIndex = comBox.SelectedIndex;
  103. if (IsValueContent == false)
  104. {
  105. Value = item.Value;
  106. ValueChanged?.Invoke(Value, null);
  107. }
  108. else
  109. {
  110. ValueStr = item.ValueStr;
  111. ValueChanged?.Invoke(ValueStr, null);
  112. }
  113. }
  114. }
  115. public double Value
  116. {
  117. get { return (double)GetValue(ValueProperty); }
  118. set { SetValue(ValueProperty, value); }
  119. }
  120. public string ValueStr
  121. {
  122. get { return (string)GetValue(ValueStrProperty); }
  123. set { SetValue(ValueStrProperty, value); }
  124. }
  125. //下拉框每项的值是否为字符串类型
  126. public bool IsValueContent
  127. {
  128. get { return (bool)GetValue(IsValueContentProperty); }
  129. set { SetValue(IsValueContentProperty, value); }
  130. }
  131. public List<ComboDataItem> ItemSource
  132. {
  133. get { return (List<ComboDataItem>)GetValue(ItemSourceProperty); }
  134. set { SetValue(ItemSourceProperty, value); }
  135. }
  136. public int SelectedIndex
  137. {
  138. get { return (int)GetValue(SelectedIndexProperty); }
  139. set { SetValue(SelectedIndexProperty, value); }
  140. }
  141. public ComboDataItem SelectedItems
  142. {
  143. get { return (ComboDataItem)GetValue(SelectedItemsProperty); }
  144. set { SetValue(SelectedItemsProperty, value); }
  145. }
  146. public static readonly DependencyProperty SelectedItemsProperty =
  147. DependencyProperty.Register("SelectedItems", typeof(ComboDataItem), typeof(CustomComboControl), new PropertyMetadata(null, SelectedItemsPropertyChanged));
  148. public static readonly DependencyProperty ValueProperty =
  149. DependencyProperty.Register("Value", typeof(double), typeof(CustomComboControl), new PropertyMetadata(1.0));
  150. public static readonly DependencyProperty ValueStrProperty =
  151. DependencyProperty.Register("ValueStr", typeof(string), typeof(CustomComboControl), new PropertyMetadata(""));
  152. public static readonly DependencyProperty ItemSourceProperty =
  153. DependencyProperty.Register("ItemSource", typeof(List<ComboDataItem>), typeof(CustomComboControl), new PropertyMetadata(null, SelectedItemSourcePropertyChanged));
  154. public static readonly DependencyProperty SelectedIndexProperty =
  155. DependencyProperty.Register("SelectedIndex", typeof(int), typeof(CustomComboControl), new PropertyMetadata(-1, SelectedIndexPropertyChanged));
  156. public static readonly DependencyProperty IsValueContentProperty =
  157. DependencyProperty.Register("IsValueContent", typeof(bool), typeof(CustomComboControl), new PropertyMetadata(false));
  158. //集合绑定下拉框 SelectedItems触发属性
  159. private static void SelectedItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  160. {
  161. var control = d as CustomComboControl;
  162. var selectedItems = (ComboDataItem)e.NewValue;
  163. if (control != null & selectedItems != null)
  164. {
  165. if(control.comBox.Items != null && control.comBox.Items.Count > 0)
  166. {
  167. if(control.IsValueContent)
  168. {
  169. control.ValueStr = selectedItems.ValueStr;
  170. }
  171. else
  172. {
  173. control.Value = selectedItems.Value;
  174. }
  175. }
  176. if(control.comBox.Items == null || control.comBox.Items.Count == 0)
  177. {
  178. control.title.Text = selectedItems.Content;
  179. }
  180. else
  181. {
  182. int index = -1;
  183. ComboDataItem temp;
  184. if (control.IsValueContent)
  185. temp = control.Items.FirstOrDefault(inlineItem => inlineItem.ValueStr == selectedItems.ValueStr);
  186. else
  187. temp = control.Items.FirstOrDefault(inlineItem => inlineItem.Value == selectedItems.Value);
  188. if (temp != null)
  189. index = control.Items.IndexOf(temp);
  190. if (index >= 0)
  191. {
  192. control.SelectedIndex = -1;
  193. control.SelectedIndex = index;
  194. }
  195. else
  196. control.title.Text = selectedItems.Content;
  197. }
  198. }
  199. }
  200. //集合绑定下拉框Itemsource触发属性
  201. private static void SelectedItemSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  202. {
  203. var control = d as CustomComboControl;
  204. var itemsource = (List<ComboDataItem>)e.NewValue;
  205. if (control != null)
  206. {
  207. control.SelectedIndex = -1;
  208. control.comBox.ItemsSource = itemsource;
  209. control.Items = itemsource;
  210. control.SelectedIndex = ((itemsource == null || itemsource.Count == 0) ? -1 : 0);
  211. }
  212. }
  213. //选中项触发属性
  214. private static void SelectedIndexPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  215. {
  216. var control = d as CustomComboControl;
  217. var selectedIndex = (int)e.NewValue;
  218. if (control != null)
  219. {
  220. if(control.comBox.Items != null && control.comBox.Items.Count > 0 && selectedIndex != -1)
  221. {
  222. control.comBox.SelectedIndex = selectedIndex;
  223. if (control.comBox.SelectedItem != null)
  224. control.SelectedItems = (ComboDataItem)control.comBox.SelectedItem;
  225. else
  226. control.SelectedItems = null;
  227. }
  228. control.UpdateSelectedIndex();
  229. }
  230. }
  231. //选中项后,更新控件选中的显示内容
  232. public void UpdateSelectedIndex()
  233. {
  234. if(SelectedIndex < 0 || comBox.Items == null || comBox.Items.Count <= SelectedIndex)
  235. {
  236. title.Text = "";
  237. }
  238. else
  239. {
  240. title.Text = Items[SelectedIndex].Content;
  241. }
  242. }
  243. }
  244. }