CustomComboControl.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. using Microsoft.Office.Interop.Word;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace PDF_Master.CustomControl.CompositeControl
  18. {
  19. //用法:
  20. // IsValueContent为ture时,使用ValueStr作为集合某项的值
  21. //IsValueContent为false时,使用Value作为集合某项的值
  22. //SetItemSource(List<ComboDataItem> items)更换集合List
  23. //默认:字体集合、IsValueContent为false、Value作为集合某项的值
  24. public class ComboDataItem : Prism.Mvvm.BindableBase
  25. {
  26. //private bool needFrontTag = false;
  27. //public bool NeedFrontTag
  28. //{
  29. // get { return needFrontTag; }
  30. // set
  31. // {
  32. // SetProperty(ref needFrontTag, value);
  33. // }
  34. //}
  35. //字符串类型的值
  36. //public string ValueStr { get; set; }
  37. private string valueStr;
  38. public string ValueStr
  39. {
  40. get
  41. {
  42. return valueStr;
  43. }
  44. set
  45. {
  46. SetProperty(ref valueStr, value);
  47. }
  48. }
  49. //数字类型的值
  50. public double Value { get; private set; }
  51. //public string Content { get; private set; }
  52. //下拉框显示的内容
  53. private string _content = "";
  54. public string Content
  55. {
  56. get { return _content; }
  57. private set { SetProperty(ref _content, value); }
  58. }
  59. private FontFamily fontFamily = new FontFamily("Arial");
  60. public FontFamily FontFamily
  61. {
  62. get { return fontFamily; }
  63. set { SetProperty(ref fontFamily, value); }
  64. }
  65. //下拉框显示的内容+单位:限数字值的单位
  66. public string Unit { get; private set; }
  67. //数字类型
  68. public ComboDataItem(double value, string unitStr = "")
  69. {
  70. Content = value + unitStr;
  71. Value = value;
  72. }
  73. //字符串类型
  74. public ComboDataItem(string valueStr, string contentStr = "")
  75. {
  76. Content = contentStr;
  77. ValueStr = valueStr;
  78. //this.NeedFrontTag = needFrontTag;
  79. }
  80. public void SetContent(string content)
  81. {
  82. Content = content;
  83. }
  84. public static explicit operator double(ComboDataItem v)
  85. {
  86. throw new NotImplementedException();
  87. }
  88. }
  89. /// <summary>
  90. /// CustomComboControl.xaml 的交互逻辑
  91. /// </summary>
  92. public partial class CustomComboControl : UserControl
  93. {
  94. public List<ComboDataItem> Items { get; private set; }
  95. public event RoutedEventHandler ValueChanged;
  96. //private string titleStr;
  97. //public string Title
  98. //{
  99. // get
  100. // {
  101. // titleStr = title.Text;
  102. // return titleStr;
  103. // }
  104. // set
  105. // {
  106. // title.Text = titleStr;
  107. // }
  108. //}
  109. public CustomComboControl()
  110. {
  111. InitializeComponent();
  112. Items = new List<ComboDataItem>();
  113. DefaultItems();
  114. }
  115. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  116. {
  117. comBox.SelectionChanged -= comBox_SelectionChanged;
  118. comBox.SelectionChanged += comBox_SelectionChanged;
  119. }
  120. //更换集合
  121. public void SetItemSource(List<ComboDataItem> items)
  122. {
  123. SelectedIndex = -1;//为了触发SelectedIndex
  124. Items = items;
  125. ItemSource = Items;
  126. }
  127. /// <summary>
  128. /// 默认集合(字体集合)
  129. /// </summary>
  130. private void DefaultItems()
  131. {
  132. Items.Clear();
  133. SelectedIndex = -1;
  134. var item = new ComboDataItem(3);
  135. Items.Add(item);
  136. item = new ComboDataItem(6);
  137. Items.Add(item);
  138. item = new ComboDataItem(9);
  139. Items.Add(item);
  140. item = new ComboDataItem(12);
  141. Items.Add(item);
  142. item = new ComboDataItem(15);
  143. Items.Add(item);
  144. item = new ComboDataItem(18);
  145. Items.Add(item);
  146. item = new ComboDataItem(21);
  147. Items.Add(item);
  148. item = new ComboDataItem(24);
  149. Items.Add(item);
  150. ItemSource = Items;
  151. }
  152. //下拉框选中项
  153. private void comBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  154. {
  155. var item = comBox.SelectedItem as ComboDataItem;
  156. if (item != null)
  157. {
  158. title.Text = item.Content;
  159. Trace.WriteLine("comBox_SelectionChanged" + title.Text);
  160. if (SelectedIndex != comBox.SelectedIndex)
  161. {
  162. SelectedIndex = comBox.SelectedIndex;
  163. }
  164. if (IsValueContent == false)
  165. {
  166. Value = item.Value;
  167. ValueChanged?.Invoke(Value, null);
  168. }
  169. else
  170. {
  171. ValueStr = item.ValueStr;
  172. ValueChanged?.Invoke(ValueStr, null);
  173. }
  174. }
  175. }
  176. //在外部判断值是否存在于下拉框里
  177. public bool IsExistInComBox(object value)
  178. {
  179. if (value is double)
  180. {
  181. var item = Items.FirstOrDefault(temp => temp.Value == (double)Value);
  182. return (item == null ? false : true);
  183. }
  184. else if (value is string)
  185. {
  186. var item = Items.FirstOrDefault(temp => temp.ValueStr == (string)ValueStr);
  187. return (item == null ? false : true);
  188. }
  189. return false;
  190. }
  191. public double Value
  192. {
  193. get { return (double)GetValue(ValueProperty); }
  194. set { SetValue(ValueProperty, value); }
  195. }
  196. public string ValueStr
  197. {
  198. get { return (string)GetValue(ValueStrProperty); }
  199. set { SetValue(ValueStrProperty, value); }
  200. }
  201. //下拉框每项的值是否为字符串类型
  202. public bool IsValueContent
  203. {
  204. get { return (bool)GetValue(IsValueContentProperty); }
  205. set { SetValue(IsValueContentProperty, value); }
  206. }
  207. public List<ComboDataItem> ItemSource
  208. {
  209. get { return (List<ComboDataItem>)GetValue(ItemSourceProperty); }
  210. set { SetValue(ItemSourceProperty, value); }
  211. }
  212. public int SelectedIndex
  213. {
  214. get { return (int)GetValue(SelectedIndexProperty); }
  215. set { SetValue(SelectedIndexProperty, value); }
  216. }
  217. public ComboDataItem SelectedItems
  218. {
  219. get { return (ComboDataItem)GetValue(SelectedItemsProperty); }
  220. set { SetValue(SelectedItemsProperty, value); }
  221. }
  222. //标题
  223. public string Title
  224. {
  225. get { return (string)GetValue(TitleProperty); }
  226. set { SetValue(TitleProperty, value); }
  227. }
  228. public static readonly DependencyProperty TitleProperty =
  229. DependencyProperty.Register("Title", typeof(string), typeof(CustomComboControl), new PropertyMetadata("", TitlePropertyChanged));
  230. private static void TitlePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  231. {
  232. var control = d as CustomComboControl;
  233. var title = (string)e.NewValue;
  234. if (control != null)
  235. {
  236. control.title.Text = title;
  237. }
  238. }
  239. //外部需要下拉框都不选中时,true为不选中,false为正常选中
  240. public bool IsSelectedEmpty
  241. {
  242. get { return (bool)GetValue(IsSelectedEmptyProperty); }
  243. set { SetValue(IsSelectedEmptyProperty, value); }
  244. }
  245. public static readonly DependencyProperty IsSelectedEmptyProperty =
  246. DependencyProperty.Register("IsSelectedEmpty", typeof(bool), typeof(CustomComboControl), new PropertyMetadata(false, IsSelectedEmptyPropertyChanged));
  247. public static readonly DependencyProperty SelectedItemsProperty =
  248. DependencyProperty.Register("SelectedItems", typeof(ComboDataItem), typeof(CustomComboControl), new PropertyMetadata(null, SelectedItemsPropertyChanged));
  249. public static readonly DependencyProperty ValueProperty =
  250. DependencyProperty.Register("Value", typeof(double), typeof(CustomComboControl), new PropertyMetadata(1.0));
  251. public static readonly DependencyProperty ValueStrProperty =
  252. DependencyProperty.Register("ValueStr", typeof(string), typeof(CustomComboControl), new PropertyMetadata(""));
  253. public static readonly DependencyProperty ItemSourceProperty =
  254. DependencyProperty.Register("ItemSource", typeof(List<ComboDataItem>), typeof(CustomComboControl), new PropertyMetadata(null, SelectedItemSourcePropertyChanged));
  255. public static readonly DependencyProperty SelectedIndexProperty =
  256. DependencyProperty.Register("SelectedIndex", typeof(int), typeof(CustomComboControl), new PropertyMetadata(-1, SelectedIndexPropertyChanged));
  257. public static readonly DependencyProperty IsValueContentProperty =
  258. DependencyProperty.Register("IsValueContent", typeof(bool), typeof(CustomComboControl), new PropertyMetadata(false));
  259. //集合绑定下拉框 SelectedItems触发属性
  260. private static void IsSelectedEmptyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  261. {
  262. var control = d as CustomComboControl;
  263. var isSelectedEmpty = (bool)e.NewValue;
  264. if (control != null)
  265. {
  266. //弃用:IsSelectedEmpty属性,可用SelectedItems == null来处理下拉框不选中的情况。
  267. //if(isSelectedEmpty)
  268. //{
  269. // control.comBox.SelectedItem = null;
  270. // control.SelectedIndex = -1;
  271. // control.SelectedItems = null;
  272. // control.title.Text = "";
  273. //}
  274. }
  275. }
  276. private static void SelectedItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  277. {
  278. var control = d as CustomComboControl;
  279. var selectedItems = (ComboDataItem)e.NewValue;
  280. if (control != null /* && control.IsSelectedEmpty == false*/)
  281. {
  282. if (selectedItems != null)
  283. {
  284. if (control.comBox.Items != null && control.comBox.Items.Count > 0)
  285. {
  286. if (control.IsValueContent)
  287. {
  288. control.ValueStr = selectedItems.ValueStr;
  289. }
  290. else
  291. {
  292. control.Value = selectedItems.Value;
  293. Trace.WriteLine("SelectedItemsPropertyChangedValue" + control.Value);
  294. }
  295. control.title.Text = selectedItems.Content;
  296. }
  297. if (control.comBox.Items == null || control.comBox.Items.Count == 0)
  298. {
  299. control.title.Text = selectedItems.Content;
  300. }
  301. else
  302. {
  303. int index = -1;
  304. ComboDataItem temp;
  305. if (control.IsValueContent)
  306. temp = control.Items.FirstOrDefault(inlineItem => inlineItem.ValueStr == selectedItems.ValueStr);
  307. else
  308. temp = control.Items.FirstOrDefault(inlineItem => inlineItem.Value == selectedItems.Value);
  309. if (temp != null)
  310. index = control.Items.IndexOf(temp);
  311. if (index >= 0)
  312. {
  313. //为了改变值时选项跟着改变,但是值并没有时时刻刻改变,且影响其他事件展示注释掉 2023/4/4
  314. //if (control.SelectedIndex != index)
  315. //{
  316. // control.SelectedIndex = index;
  317. // Trace.WriteLine("SelectedItemsPropertyChanged" + index);
  318. //}
  319. //control.SelectedIndex = -1;
  320. //control.SelectedIndex = index;
  321. }
  322. else
  323. control.title.Text = selectedItems.Content;
  324. }
  325. }
  326. else
  327. {
  328. control.comBox.SelectedItem = null;
  329. control.SelectedIndex = -1;
  330. control.SelectedItems = null;
  331. control.title.Text = "";
  332. }
  333. }
  334. }
  335. //集合绑定下拉框Itemsource触发属性
  336. private static void SelectedItemSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  337. {
  338. var control = d as CustomComboControl;
  339. var itemsource = (List<ComboDataItem>)e.NewValue;
  340. if (control != null /*& control.IsSelectedEmpty == false*/)
  341. {
  342. control.SelectedIndex = -1;
  343. control.comBox.ItemsSource = itemsource;
  344. control.Items = itemsource;
  345. control.SelectedIndex = ((itemsource == null || itemsource.Count == 0) ? -1 : 0);
  346. }
  347. }
  348. //选中项触发属性
  349. private static void SelectedIndexPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  350. {
  351. var control = d as CustomComboControl;
  352. var selectedIndex = (int)e.NewValue;
  353. if (control != null /*&& control.IsSelectedEmpty == false*/)
  354. {
  355. //删掉了selectedIndex != -1,设置不选中任何选项的情况
  356. //删掉了control.SelectedItems = null;避免崩溃
  357. if (control.comBox.Items != null && control.comBox.Items.Count > 0)
  358. {
  359. control.comBox.SelectedIndex = selectedIndex;
  360. if (control.comBox.SelectedItem != null)
  361. {
  362. if (control.comBox.SelectedItem is ComboDataItem comboDataItem)
  363. {
  364. bool isEqual = EqualityComparer<ComboDataItem>.Default.Equals(comboDataItem, control.SelectedItems);
  365. if (isEqual == false)
  366. {
  367. control.SelectedItems = (ComboDataItem)control.comBox.SelectedItem;
  368. }
  369. }
  370. }
  371. }
  372. control.UpdateSelectedIndex();
  373. }
  374. }
  375. //选中项后,更新控件选中的显示内容
  376. public void UpdateSelectedIndex()
  377. {
  378. if (SelectedIndex < 0 || comBox.Items == null || comBox.Items.Count <= SelectedIndex)
  379. {
  380. title.Text = "";
  381. }
  382. else
  383. {
  384. if (IsSelectedEmpty == false)
  385. title.Text = Items[SelectedIndex].Content;
  386. }
  387. }
  388. }
  389. }