SlidComboControl.xaml.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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(0));
  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. }
  165. }
  166. public List<ComboDataItem> ItemsSource
  167. {
  168. get { return (List<ComboDataItem>)GetValue(ItemsSourceProperty); }
  169. set { SetValue(ItemsSourceProperty, value); }
  170. }
  171. public static readonly DependencyProperty ItemsSourceProperty =
  172. DependencyProperty.Register("ItemsSource", typeof(List<ComboDataItem>), typeof(SlidComboControl), new PropertyMetadata(null, SelectedItemsSourcePropertyChanged));
  173. private static void SelectedItemsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  174. {
  175. var control = d as SlidComboControl;
  176. var value = (List<ComboDataItem>)e.NewValue;
  177. if (control != null && value != null)
  178. {
  179. control.SetItemSource(value);
  180. }
  181. }
  182. private void ThicknessSlider_PreviewMouseMove(object sender, MouseEventArgs e)
  183. {
  184. if(e.LeftButton == MouseButtonState.Pressed)
  185. {
  186. ValueChanged?.Invoke(ThicknessSlider.Value, null);
  187. }
  188. }
  189. }
  190. }