CustomComboControl.xaml.cs 14 KB

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