SlidComboControl.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using Prism.Commands;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace PDF_Office.CustomControl.CompositeControl
  17. {
  18. /// <summary>
  19. /// SlidComboControl.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class SlidComboControl : UserControl
  22. {
  23. public List<ComboDataItem> Items { get;private set; }
  24. public event RoutedEventHandler ValueChanged;
  25. public SlidComboControl()
  26. {
  27. InitializeComponent();
  28. Items = new List<ComboDataItem>();
  29. DefaultItems();
  30. }
  31. public void SetItemSource(List<ComboDataItem> items)
  32. {
  33. Items = items;
  34. combox.ItemSource = Items;
  35. UpdateMaxMinValue(items);
  36. }
  37. private void UpdateMaxMinValue(List<ComboDataItem> items)
  38. {
  39. double MaxValue = 0;
  40. double MinValue = 0;
  41. if (items != null)
  42. {
  43. foreach (var item in items)
  44. {
  45. if (item.Value >= MaxValue)
  46. MaxValue = item.Value;
  47. }
  48. MinValue = MaxValue;
  49. foreach (var item in items)
  50. {
  51. if (item.Value <= MinValue)
  52. MinValue = item.Value;
  53. }
  54. ThicknessSlider.Maximum = MaxValue;
  55. ThicknessSlider.Minimum = MinValue;
  56. }
  57. }
  58. private void DefaultItems()
  59. {
  60. var item = new ComboDataItem(3);
  61. Items.Add(item);
  62. item = new ComboDataItem(6);
  63. Items.Add(item);
  64. item = new ComboDataItem(9);
  65. Items.Add(item);
  66. item = new ComboDataItem(12);
  67. Items.Add(item);
  68. item = new ComboDataItem(15);
  69. Items.Add(item);
  70. item = new ComboDataItem(18);
  71. Items.Add(item);
  72. item = new ComboDataItem(21);
  73. Items.Add(item);
  74. item = new ComboDataItem(24);
  75. Items.Add(item);
  76. combox.ItemSource = Items;
  77. UpdateMaxMinValue(Items);
  78. }
  79. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  80. {
  81. ThicknessSlider.ValueChanged += ThicknessSlider_ValueChanged;
  82. combox.ValueChanged -= combox_ValueChanged;
  83. combox.ValueChanged += combox_ValueChanged;
  84. }
  85. private void combox_ValueChanged(object sender, RoutedEventArgs e)
  86. {
  87. if (sender is double == true)
  88. {
  89. //点击下拉框时触发
  90. if (combox.IsExistInComBox(sender))
  91. {
  92. ThicknessSlider.Value = (double)sender;
  93. ValueChanged?.Invoke((double)sender, null);
  94. }
  95. }
  96. }
  97. private void ThicknessSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  98. {
  99. combox.SelectedItems = new ComboDataItem(Value);
  100. // ValueChanged?.Invoke(Value, null);
  101. }
  102. public double Value
  103. {
  104. get { return (double)GetValue(ValueProperty); }
  105. set { SetValue(ValueProperty, value); }
  106. }
  107. public static readonly DependencyProperty ValueProperty =
  108. DependencyProperty.Register("Value", typeof(double), typeof(SlidComboControl), new PropertyMetadata(1.0, SelectedValuePropertyChanged));
  109. private static void SelectedValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  110. {
  111. var control = d as SlidComboControl;
  112. var value = (double)e.NewValue;
  113. if (control != null)
  114. {
  115. control.combox.SelectedItems = new ComboDataItem(value);
  116. }
  117. }
  118. public List<ComboDataItem> ItemsSource
  119. {
  120. get { return (List<ComboDataItem>)GetValue(ItemsSourceProperty); }
  121. set { SetValue(ItemsSourceProperty, value); }
  122. }
  123. public static readonly DependencyProperty ItemsSourceProperty =
  124. DependencyProperty.Register("ItemsSource", typeof(List<ComboDataItem>), typeof(SlidComboControl), new PropertyMetadata(null, SelectedItemsSourcePropertyChanged));
  125. private static void SelectedItemsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  126. {
  127. var control = d as SlidComboControl;
  128. var value = (List<ComboDataItem>)e.NewValue;
  129. if (control != null && value != null)
  130. {
  131. control.SetItemSource(value);
  132. }
  133. }
  134. private void ThicknessSlider_PreviewMouseMove(object sender, MouseEventArgs e)
  135. {
  136. if(e.LeftButton == MouseButtonState.Pressed)
  137. {
  138. ValueChanged?.Invoke(ThicknessSlider.Value, null);
  139. }
  140. }
  141. }
  142. }