SlidComboControl.xaml.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using Microsoft.Office.Interop.Word;
  2. using Prism.Commands;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. namespace PDF_Master.CustomControl.CompositeControl
  19. {
  20. /// <summary>
  21. /// SlidComboControl.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class SlidComboControl : UserControl
  24. {
  25. public List<ComboDataItem> Items { get;private set; }
  26. public event RoutedEventHandler ValueChanged;
  27. public string Title
  28. {
  29. get { return (string)GetValue(TitleProperty); }
  30. set {
  31. SetValue(TitleProperty, value); }
  32. }
  33. public static readonly DependencyProperty TitleProperty =
  34. DependencyProperty.Register("Title", typeof(string), typeof(SlidComboControl), new PropertyMetadata("Line Width"));
  35. //public int IndexSelected
  36. //{
  37. // get { return (int)GetValue(IndexSelectedProperty); }
  38. // set
  39. // {
  40. // SetValue(IndexSelectedProperty, value);
  41. // }
  42. //}
  43. //public static readonly DependencyProperty IndexSelectedProperty =
  44. // DependencyProperty.Register("IndexSelected", typeof(int), typeof(SlidComboControl), new PropertyMetadata(0));
  45. public string Unit
  46. {
  47. get { return (string)GetValue(UnitProperty); }
  48. set { SetValue(UnitProperty, value); }
  49. }
  50. public static readonly DependencyProperty UnitProperty =
  51. DependencyProperty.Register("Unit", typeof(string), typeof(SlidComboControl), new PropertyMetadata("pt"));
  52. public SlidComboControl()
  53. {
  54. InitializeComponent();
  55. Items = new List<ComboDataItem>();
  56. DefaultItems();
  57. SetLanguage();
  58. }
  59. private void SetLanguage()
  60. {
  61. //焊死了
  62. if(thicknessText.Text== "Line Width")
  63. {
  64. thicknessText.Text = App.MainPageLoader.GetString("Annotation_Thickness");
  65. }
  66. }
  67. public void SetItemSource(List<ComboDataItem> items)
  68. {
  69. Items = items;
  70. combox.ItemSource = Items;
  71. UpdateMaxMinValue(items);
  72. }
  73. private void UpdateMaxMinValue(List<ComboDataItem> items)
  74. {
  75. double MaxValue = 0;
  76. double MinValue = 0;
  77. if (items != null)
  78. {
  79. foreach (var item in items)
  80. {
  81. if (item.Value >= MaxValue)
  82. MaxValue = item.Value;
  83. }
  84. MinValue = MaxValue;
  85. foreach (var item in items)
  86. {
  87. if (item.Value <= MinValue)
  88. MinValue = item.Value;
  89. }
  90. ThicknessSlider.Maximum = MaxValue;
  91. ThicknessSlider.Minimum = MinValue;
  92. }
  93. }
  94. private void DefaultItems()
  95. {
  96. var item = new ComboDataItem(3);
  97. Items.Add(item);
  98. item = new ComboDataItem(6);
  99. Items.Add(item);
  100. item = new ComboDataItem(9);
  101. Items.Add(item);
  102. item = new ComboDataItem(12);
  103. Items.Add(item);
  104. item = new ComboDataItem(15);
  105. Items.Add(item);
  106. item = new ComboDataItem(18);
  107. Items.Add(item);
  108. item = new ComboDataItem(21);
  109. Items.Add(item);
  110. item = new ComboDataItem(24);
  111. Items.Add(item);
  112. combox.ItemSource = Items;
  113. UpdateMaxMinValue(Items);
  114. }
  115. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  116. {
  117. //ThicknessSlider.ValueChanged += ThicknessSlider_ValueChanged;
  118. combox.ValueChanged -= combox_ValueChanged;
  119. combox.ValueChanged += combox_ValueChanged;
  120. }
  121. private void combox_ValueChanged(object sender, RoutedEventArgs e)
  122. {
  123. if (sender is double == true)
  124. {
  125. //点击下拉框时触发
  126. if (combox.IsExistInComBox(sender))
  127. {
  128. Trace.WriteLine("combox_ValueChanged" + (double)sender);
  129. ThicknessSlider.Value = (double)sender;
  130. ValueChanged?.Invoke((double)sender, null);
  131. }
  132. }
  133. }
  134. //事件重复SelectedValuePropertyChanged 2023/4/4
  135. //private void ThicknessSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  136. //{
  137. // combox.SelectedItems = new ComboDataItem(Value);
  138. // // ValueChanged?.Invoke(Value, null);
  139. //}
  140. //暂无作用,后续准备
  141. public int SelectedIndex
  142. {
  143. get { return (int)GetValue(SelectedIndexProperty); }
  144. set { SetValue(SelectedIndexProperty, value);
  145. }
  146. }
  147. public static readonly DependencyProperty SelectedIndexProperty =
  148. DependencyProperty.Register("SelectedIndex", typeof(int), typeof(SlidComboControl), new PropertyMetadata(-1, SelectedIndexPropertyChanged));
  149. public double Value
  150. {
  151. get { return (double)GetValue(ValueProperty); }
  152. set { SetValue(ValueProperty, value); }
  153. }
  154. public static readonly DependencyProperty ValueProperty =
  155. DependencyProperty.Register("Value", typeof(double), typeof(SlidComboControl), new PropertyMetadata(1.0, SelectedValuePropertyChanged));
  156. private static void SelectedValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  157. {
  158. var control = d as SlidComboControl;
  159. var value = (double)e.NewValue;
  160. if (control != null)
  161. {
  162. Trace.WriteLine("SelectedValuePropertyChanged" + value);
  163. control.combox.SelectedItems = new ComboDataItem(value, control.Unit);
  164. //判断滑动条的值是否与combox中的项匹配
  165. if (control != null && control.combox.ItemSource != null)
  166. {
  167. for (int i = 0; i < control.combox.ItemSource.Count; i++)
  168. {
  169. if (value == control.combox.ItemSource[i].Value)
  170. {
  171. return;
  172. }
  173. if(i==control.combox.ItemSource.Count-1)
  174. {
  175. control.combox.SelectedIndex = -1;
  176. control.combox.SelectedItems = new ComboDataItem(value, control.Unit);
  177. }
  178. }
  179. }
  180. }
  181. }
  182. public List<ComboDataItem> ItemsSource
  183. {
  184. get { return (List<ComboDataItem>)GetValue(ItemsSourceProperty); }
  185. set { SetValue(ItemsSourceProperty, value); }
  186. }
  187. public static readonly DependencyProperty ItemsSourceProperty =
  188. DependencyProperty.Register("ItemsSource", typeof(List<ComboDataItem>), typeof(SlidComboControl), new PropertyMetadata(null, SelectedItemsSourcePropertyChanged));
  189. //选中项触发属性
  190. private static void SelectedIndexPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  191. {
  192. var control = d as SlidComboControl;
  193. var selectedIndex = (int)e.NewValue;
  194. if (control != null /*&& control.IsSelectedEmpty == false*/)
  195. {
  196. //删掉了selectedIndex != -1,设置不选中任何选项的情况
  197. //删掉了control.SelectedItems = null;避免崩溃
  198. if (control.combox.Items != null && control.combox.Items.Count > 0)
  199. {
  200. control.combox.SelectedIndex = selectedIndex;
  201. if (control.combox.SelectedItems != null)
  202. control.combox.SelectedItems = (ComboDataItem)control.combox.SelectedItems;
  203. }
  204. control.combox.UpdateSelectedIndex();
  205. }
  206. }
  207. private static void SelectedItemsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  208. {
  209. var control = d as SlidComboControl;
  210. var value = (List<ComboDataItem>)e.NewValue;
  211. if (control != null && value != null)
  212. {
  213. control.SetItemSource(value);
  214. }
  215. }
  216. private void ThicknessSlider_PreviewMouseMove(object sender, MouseEventArgs e)
  217. {
  218. if(e.LeftButton == MouseButtonState.Pressed)
  219. {
  220. ValueChanged?.Invoke(ThicknessSlider.Value, null);
  221. }
  222. }
  223. }
  224. }