CustomComboControl.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 : Prism.Mvvm.BindableBase
  23. {
  24. //字符串类型的值
  25. public string ValueStr { get; private set; }
  26. //数字类型的值
  27. public double Value { get; private set; }
  28. //public string Content { get; private set; }
  29. //下拉框显示的内容
  30. private string _content = "";
  31. public string Content
  32. {
  33. get { return _content; }
  34. private set { SetProperty(ref _content, value); }
  35. }
  36. //下拉框显示的内容+单位:限数字值的单位
  37. public string Unit { get; private set; }
  38. //数字类型
  39. public ComboDataItem(double value, string unitStr = "")
  40. {
  41. Content = value + unitStr;
  42. Value = value;
  43. }
  44. //字符串类型
  45. public ComboDataItem(string valueStr, string contentStr = "")
  46. {
  47. Content = contentStr;
  48. ValueStr = valueStr;
  49. }
  50. public void SetContent(string content)
  51. {
  52. Content = content;
  53. }
  54. }
  55. /// <summary>
  56. /// CustomComboControl.xaml 的交互逻辑
  57. /// </summary>
  58. public partial class CustomComboControl : UserControl
  59. {
  60. public List<ComboDataItem> Items { get; private set; }
  61. public event RoutedEventHandler ValueChanged;
  62. public CustomComboControl()
  63. {
  64. InitializeComponent();
  65. Items = new List<ComboDataItem>();
  66. DefaultItems();
  67. }
  68. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  69. {
  70. comBox.SelectionChanged -= comBox_SelectionChanged;
  71. comBox.SelectionChanged += comBox_SelectionChanged;
  72. }
  73. //更换集合
  74. public void SetItemSource(List<ComboDataItem> items)
  75. {
  76. SelectedIndex = - 1;//为了触发SelectedIndex
  77. Items = items;
  78. ItemSource = Items;
  79. }
  80. /// <summary>
  81. /// 默认集合(字体集合)
  82. /// </summary>
  83. private void DefaultItems()
  84. {
  85. Items.Clear();
  86. SelectedIndex = -1;
  87. var item = new ComboDataItem(3);
  88. Items.Add(item);
  89. item = new ComboDataItem(6);
  90. Items.Add(item);
  91. item = new ComboDataItem(9);
  92. Items.Add(item);
  93. item = new ComboDataItem(12);
  94. Items.Add(item);
  95. item = new ComboDataItem(15);
  96. Items.Add(item);
  97. item = new ComboDataItem(18);
  98. Items.Add(item);
  99. item = new ComboDataItem(21);
  100. Items.Add(item);
  101. item = new ComboDataItem(24);
  102. Items.Add(item);
  103. ItemSource = Items;
  104. }
  105. //下拉框选中项
  106. private void comBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  107. {
  108. var item = comBox.SelectedItem as ComboDataItem;
  109. if (item != null)
  110. {
  111. title.Text = item.Content;
  112. SelectedIndex = comBox.SelectedIndex;
  113. if (IsValueContent == false)
  114. {
  115. Value = item.Value;
  116. ValueChanged?.Invoke(Value, null);
  117. }
  118. else
  119. {
  120. ValueStr = item.ValueStr;
  121. ValueChanged?.Invoke(ValueStr, null);
  122. }
  123. }
  124. }
  125. //在外部判断值是否存在于下拉框里
  126. public bool IsExistInComBox(object value)
  127. {
  128. if (value is double)
  129. {
  130. var item = Items.FirstOrDefault(temp => temp.Value == (double)Value);
  131. return (item == null ? false : true);
  132. }
  133. else if (value is string)
  134. {
  135. var item = Items.FirstOrDefault(temp => temp.ValueStr == (string)ValueStr);
  136. return (item == null ? false : true);
  137. }
  138. return false;
  139. }
  140. public double Value
  141. {
  142. get { return (double)GetValue(ValueProperty); }
  143. set { SetValue(ValueProperty, value); }
  144. }
  145. public string ValueStr
  146. {
  147. get { return (string)GetValue(ValueStrProperty); }
  148. set { SetValue(ValueStrProperty, value); }
  149. }
  150. //下拉框每项的值是否为字符串类型
  151. public bool IsValueContent
  152. {
  153. get { return (bool)GetValue(IsValueContentProperty); }
  154. set { SetValue(IsValueContentProperty, value); }
  155. }
  156. public List<ComboDataItem> ItemSource
  157. {
  158. get { return (List<ComboDataItem>)GetValue(ItemSourceProperty); }
  159. set { SetValue(ItemSourceProperty, value); }
  160. }
  161. public int SelectedIndex
  162. {
  163. get { return (int)GetValue(SelectedIndexProperty); }
  164. set { SetValue(SelectedIndexProperty, value); }
  165. }
  166. public ComboDataItem SelectedItems
  167. {
  168. get { return (ComboDataItem)GetValue(SelectedItemsProperty); }
  169. set { SetValue(SelectedItemsProperty, value); }
  170. }
  171. //外部需要下拉框都不选中时,true为不选中,false为正常选中
  172. public bool IsSelectedEmpty
  173. {
  174. get { return (bool)GetValue(IsSelectedEmptyProperty); }
  175. set { SetValue(IsSelectedEmptyProperty, value); }
  176. }
  177. public static readonly DependencyProperty IsSelectedEmptyProperty =
  178. DependencyProperty.Register("IsSelectedEmpty", typeof(bool), typeof(CustomComboControl), new PropertyMetadata(false, IsSelectedEmptyPropertyChanged));
  179. public static readonly DependencyProperty SelectedItemsProperty =
  180. DependencyProperty.Register("SelectedItems", typeof(ComboDataItem), typeof(CustomComboControl), new PropertyMetadata(null, SelectedItemsPropertyChanged));
  181. public static readonly DependencyProperty ValueProperty =
  182. DependencyProperty.Register("Value", typeof(double), typeof(CustomComboControl), new PropertyMetadata(1.0));
  183. public static readonly DependencyProperty ValueStrProperty =
  184. DependencyProperty.Register("ValueStr", typeof(string), typeof(CustomComboControl), new PropertyMetadata(""));
  185. public static readonly DependencyProperty ItemSourceProperty =
  186. DependencyProperty.Register("ItemSource", typeof(List<ComboDataItem>), typeof(CustomComboControl), new PropertyMetadata(null, SelectedItemSourcePropertyChanged));
  187. public static readonly DependencyProperty SelectedIndexProperty =
  188. DependencyProperty.Register("SelectedIndex", typeof(int), typeof(CustomComboControl), new PropertyMetadata(-1, SelectedIndexPropertyChanged));
  189. public static readonly DependencyProperty IsValueContentProperty =
  190. DependencyProperty.Register("IsValueContent", typeof(bool), typeof(CustomComboControl), new PropertyMetadata(false));
  191. //集合绑定下拉框 SelectedItems触发属性
  192. private static void IsSelectedEmptyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  193. {
  194. var control = d as CustomComboControl;
  195. var isSelectedEmpty = (bool)e.NewValue;
  196. if(control != null)
  197. {
  198. //弃用:IsSelectedEmpty属性,可用SelectedItems == null来处理下拉框不选中的情况。
  199. //if(isSelectedEmpty)
  200. //{
  201. // control.comBox.SelectedItem = null;
  202. // control.SelectedIndex = -1;
  203. // control.SelectedItems = null;
  204. // control.title.Text = "";
  205. //}
  206. }
  207. }
  208. private static void SelectedItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  209. {
  210. var control = d as CustomComboControl;
  211. var selectedItems = (ComboDataItem)e.NewValue;
  212. if (control != null /* && control.IsSelectedEmpty == false*/)
  213. {
  214. if(selectedItems != null)
  215. {
  216. if (control.comBox.Items != null && control.comBox.Items.Count > 0)
  217. {
  218. if (control.IsValueContent)
  219. {
  220. control.ValueStr = selectedItems.ValueStr;
  221. }
  222. else
  223. {
  224. control.Value = selectedItems.Value;
  225. }
  226. control.title.Text = selectedItems.Content;
  227. }
  228. if (control.comBox.Items == null || control.comBox.Items.Count == 0)
  229. {
  230. control.title.Text = selectedItems.Content;
  231. }
  232. else
  233. {
  234. int index = -1;
  235. ComboDataItem temp;
  236. if (control.IsValueContent)
  237. temp = control.Items.FirstOrDefault(inlineItem => inlineItem.ValueStr == selectedItems.ValueStr);
  238. else
  239. temp = control.Items.FirstOrDefault(inlineItem => inlineItem.Value == selectedItems.Value);
  240. if (temp != null)
  241. index = control.Items.IndexOf(temp);
  242. if (index >= 0)
  243. {
  244. if(control.SelectedIndex != index)
  245. {
  246. control.SelectedIndex = index;
  247. }
  248. //control.SelectedIndex = -1;
  249. //control.SelectedIndex = index;
  250. }
  251. else
  252. control.title.Text = selectedItems.Content;
  253. }
  254. }
  255. else
  256. {
  257. control.comBox.SelectedItem = null;
  258. control.SelectedIndex = -1;
  259. control.SelectedItems = null;
  260. control.title.Text = "";
  261. }
  262. }
  263. }
  264. //集合绑定下拉框Itemsource触发属性
  265. private static void SelectedItemSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  266. {
  267. var control = d as CustomComboControl;
  268. var itemsource = (List<ComboDataItem>)e.NewValue;
  269. if (control != null /*& control.IsSelectedEmpty == false*/)
  270. {
  271. control.SelectedIndex = -1;
  272. control.comBox.ItemsSource = itemsource;
  273. control.Items = itemsource;
  274. control.SelectedIndex = ((itemsource == null || itemsource.Count == 0) ? -1 : 0);
  275. }
  276. }
  277. //选中项触发属性
  278. private static void SelectedIndexPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  279. {
  280. var control = d as CustomComboControl;
  281. var selectedIndex = (int)e.NewValue;
  282. if (control != null /*&& control.IsSelectedEmpty == false*/)
  283. {
  284. if(control.comBox.Items != null && control.comBox.Items.Count > 0 && selectedIndex != -1)
  285. {
  286. control.comBox.SelectedIndex = selectedIndex;
  287. if (control.comBox.SelectedItem != null )
  288. control.SelectedItems = (ComboDataItem)control.comBox.SelectedItem;
  289. else
  290. control.SelectedItems = null;
  291. }
  292. control.UpdateSelectedIndex();
  293. }
  294. }
  295. //选中项后,更新控件选中的显示内容
  296. public void UpdateSelectedIndex()
  297. {
  298. if(SelectedIndex < 0 || comBox.Items == null || comBox.Items.Count <= SelectedIndex)
  299. {
  300. title.Text = "";
  301. }
  302. else
  303. {
  304. if (IsSelectedEmpty == false)
  305. title.Text = Items[SelectedIndex].Content;
  306. }
  307. }
  308. }
  309. }