CustomComboControl.xaml.cs 15 KB

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