SlidComboControl.xaml.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace PDF_Office.CustomControl.CompositeControl
  16. {
  17. /// <summary>
  18. /// SlidComboControl.xaml 的交互逻辑
  19. /// </summary>
  20. public partial class SlidComboControl : UserControl
  21. {
  22. public event EventHandler<double> ValueChanged;
  23. public SlidComboControl()
  24. {
  25. InitializeComponent();
  26. }
  27. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  28. {
  29. ThicknessBox.SelectionChanged -= ThicknessBox_SelectionChanged;
  30. ThicknessBox.SelectionChanged += ThicknessBox_SelectionChanged;
  31. ThicknessSlider.ValueChanged += ThicknessSlider_ValueChanged;
  32. ThicknessBox.SelectedIndex = 0;
  33. }
  34. private void ThicknessSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  35. {
  36. foreach(var item in ThicknessBox.Items)
  37. {
  38. var itemData = item as ComboBoxItem;
  39. if(itemData != null)
  40. {
  41. var doubleItem = double.Parse(itemData.Content.ToString());
  42. if(doubleItem == Value)
  43. {
  44. ThicknessBox.SelectedItem = itemData;
  45. break;
  46. }
  47. }
  48. }
  49. ThicknessText.Text = Value.ToString();
  50. }
  51. private void ThicknessBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  52. {
  53. var listItem = ThicknessBox.ItemContainerGenerator.ContainerFromItem(ThicknessBox.SelectedItem) as ComboBoxItem;
  54. if (listItem != null)
  55. {
  56. Value = double.Parse(listItem.Content.ToString());
  57. ThicknessText.Text = listItem.Content.ToString();
  58. }
  59. else
  60. {
  61. ThicknessText.Text = "1";
  62. Value = 1.0;
  63. }
  64. }
  65. public double Value
  66. {
  67. get { return (double)GetValue(ValueProperty); }
  68. set { SetValue(ValueProperty, value); }
  69. }
  70. public static readonly DependencyProperty ValueProperty =
  71. DependencyProperty.Register("Value", typeof(double), typeof(SlidComboControl), new PropertyMetadata(1.0, SelectedValuePropertyChanged));
  72. private static void SelectedValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  73. {
  74. var control = d as SlidComboControl;
  75. var value = (double)e.NewValue;
  76. if (control != null)
  77. {
  78. control.ValueChanged?.Invoke(control, value);
  79. }
  80. }
  81. }
  82. }