CustomComboControl.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. //在外部判断值是否存在于下拉框里
  116. public bool IsExistInComBox(object value)
  117. {
  118. if (value is double)
  119. {
  120. var item = Items.FirstOrDefault(temp => temp.Value == (double)Value);
  121. return (item == null ? false : true);
  122. }
  123. else if (value is string)
  124. {
  125. var item = Items.FirstOrDefault(temp => temp.ValueStr == (string)ValueStr);
  126. return (item == null ? false : true);
  127. }
  128. return false;
  129. }
  130. public double Value
  131. {
  132. get { return (double)GetValue(ValueProperty); }
  133. set { SetValue(ValueProperty, value); }
  134. }
  135. public string ValueStr
  136. {
  137. get { return (string)GetValue(ValueStrProperty); }
  138. set { SetValue(ValueStrProperty, value); }
  139. }
  140. //下拉框每项的值是否为字符串类型
  141. public bool IsValueContent
  142. {
  143. get { return (bool)GetValue(IsValueContentProperty); }
  144. set { SetValue(IsValueContentProperty, value); }
  145. }
  146. public List<ComboDataItem> ItemSource
  147. {
  148. get { return (List<ComboDataItem>)GetValue(ItemSourceProperty); }
  149. set { SetValue(ItemSourceProperty, value); }
  150. }
  151. public int SelectedIndex
  152. {
  153. get { return (int)GetValue(SelectedIndexProperty); }
  154. set { SetValue(SelectedIndexProperty, value); }
  155. }
  156. public ComboDataItem SelectedItems
  157. {
  158. get { return (ComboDataItem)GetValue(SelectedItemsProperty); }
  159. set { SetValue(SelectedItemsProperty, value); }
  160. }
  161. //外部需要下拉框都不选中时,true为不选中,false为正常选中
  162. public bool IsSelectedEmpty
  163. {
  164. get { return (bool)GetValue(IsSelectedEmptyProperty); }
  165. set { SetValue(IsSelectedEmptyProperty, value); }
  166. }
  167. public static readonly DependencyProperty IsSelectedEmptyProperty =
  168. DependencyProperty.Register("IsSelectedEmpty", typeof(bool), typeof(CustomComboControl), new PropertyMetadata(false, IsSelectedEmptyPropertyChanged));
  169. public static readonly DependencyProperty SelectedItemsProperty =
  170. DependencyProperty.Register("SelectedItems", typeof(ComboDataItem), typeof(CustomComboControl), new PropertyMetadata(null, SelectedItemsPropertyChanged));
  171. public static readonly DependencyProperty ValueProperty =
  172. DependencyProperty.Register("Value", typeof(double), typeof(CustomComboControl), new PropertyMetadata(1.0));
  173. public static readonly DependencyProperty ValueStrProperty =
  174. DependencyProperty.Register("ValueStr", typeof(string), typeof(CustomComboControl), new PropertyMetadata(""));
  175. public static readonly DependencyProperty ItemSourceProperty =
  176. DependencyProperty.Register("ItemSource", typeof(List<ComboDataItem>), typeof(CustomComboControl), new PropertyMetadata(null, SelectedItemSourcePropertyChanged));
  177. public static readonly DependencyProperty SelectedIndexProperty =
  178. DependencyProperty.Register("SelectedIndex", typeof(int), typeof(CustomComboControl), new PropertyMetadata(-1, SelectedIndexPropertyChanged));
  179. public static readonly DependencyProperty IsValueContentProperty =
  180. DependencyProperty.Register("IsValueContent", typeof(bool), typeof(CustomComboControl), new PropertyMetadata(false));
  181. //集合绑定下拉框 SelectedItems触发属性
  182. private static void IsSelectedEmptyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  183. {
  184. var control = d as CustomComboControl;
  185. var isSelectedEmpty = (bool)e.NewValue;
  186. if(control != null)
  187. {
  188. //弃用:IsSelectedEmpty属性,可用SelectedItems == null来处理下拉框不选中的情况。
  189. //if(isSelectedEmpty)
  190. //{
  191. // control.comBox.SelectedItem = null;
  192. // control.SelectedIndex = -1;
  193. // control.SelectedItems = null;
  194. // control.title.Text = "";
  195. //}
  196. }
  197. }
  198. private static void SelectedItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  199. {
  200. var control = d as CustomComboControl;
  201. var selectedItems = (ComboDataItem)e.NewValue;
  202. if (control != null /* && control.IsSelectedEmpty == false*/)
  203. {
  204. if(selectedItems != null)
  205. {
  206. if (control.comBox.Items != null && control.comBox.Items.Count > 0)
  207. {
  208. if (control.IsValueContent)
  209. {
  210. control.ValueStr = selectedItems.ValueStr;
  211. }
  212. else
  213. {
  214. control.Value = selectedItems.Value;
  215. }
  216. }
  217. if (control.comBox.Items == null || control.comBox.Items.Count == 0)
  218. {
  219. control.title.Text = selectedItems.Content;
  220. }
  221. else
  222. {
  223. int index = -1;
  224. ComboDataItem temp;
  225. if (control.IsValueContent)
  226. temp = control.Items.FirstOrDefault(inlineItem => inlineItem.ValueStr == selectedItems.ValueStr);
  227. else
  228. temp = control.Items.FirstOrDefault(inlineItem => inlineItem.Value == selectedItems.Value);
  229. if (temp != null)
  230. index = control.Items.IndexOf(temp);
  231. if (index >= 0)
  232. {
  233. if(control.SelectedIndex != index)
  234. {
  235. control.SelectedIndex = index;
  236. }
  237. //control.SelectedIndex = -1;
  238. //control.SelectedIndex = index;
  239. }
  240. else
  241. control.title.Text = selectedItems.Content;
  242. }
  243. }
  244. else
  245. {
  246. control.comBox.SelectedItem = null;
  247. control.SelectedIndex = -1;
  248. control.SelectedItems = null;
  249. control.title.Text = "";
  250. }
  251. }
  252. }
  253. //集合绑定下拉框Itemsource触发属性
  254. private static void SelectedItemSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  255. {
  256. var control = d as CustomComboControl;
  257. var itemsource = (List<ComboDataItem>)e.NewValue;
  258. if (control != null /*& control.IsSelectedEmpty == false*/)
  259. {
  260. control.SelectedIndex = -1;
  261. control.comBox.ItemsSource = itemsource;
  262. control.Items = itemsource;
  263. control.SelectedIndex = ((itemsource == null || itemsource.Count == 0) ? -1 : 0);
  264. }
  265. }
  266. //选中项触发属性
  267. private static void SelectedIndexPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  268. {
  269. var control = d as CustomComboControl;
  270. var selectedIndex = (int)e.NewValue;
  271. if (control != null /*&& control.IsSelectedEmpty == false*/)
  272. {
  273. if(control.comBox.Items != null && control.comBox.Items.Count > 0 && selectedIndex != -1)
  274. {
  275. control.comBox.SelectedIndex = selectedIndex;
  276. if (control.comBox.SelectedItem != null )
  277. control.SelectedItems = (ComboDataItem)control.comBox.SelectedItem;
  278. else
  279. control.SelectedItems = null;
  280. }
  281. control.UpdateSelectedIndex();
  282. }
  283. }
  284. //选中项后,更新控件选中的显示内容
  285. public void UpdateSelectedIndex()
  286. {
  287. if(SelectedIndex < 0 || comBox.Items == null || comBox.Items.Count <= SelectedIndex)
  288. {
  289. title.Text = "";
  290. }
  291. else
  292. {
  293. if (IsSelectedEmpty == false)
  294. title.Text = Items[SelectedIndex].Content;
  295. }
  296. }
  297. }
  298. }