CustomComboControl.xaml.cs 15 KB

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