CustomComboControl.xaml.cs 14 KB

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