using Prism.Commands; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace PDF_Office.CustomControl.CompositeControl { /// /// SlidComboControl.xaml 的交互逻辑 /// public partial class SlidComboControl : UserControl { public List Items { get;private set; } public event RoutedEventHandler ValueChanged; public SlidComboControl() { InitializeComponent(); Items = new List(); DefaultItems(); } public void SetItemSource(List items) { Items = items; combox.ItemSource = Items; UpdateMaxMinValue(items); } private void UpdateMaxMinValue(List items) { double MaxValue = 0; double MinValue = 0; if (items != null) { foreach (var item in items) { if (item.Value >= MaxValue) MaxValue = item.Value; } MinValue = MaxValue; foreach (var item in items) { if (item.Value <= MinValue) MinValue = item.Value; } ThicknessSlider.Maximum = MaxValue; ThicknessSlider.Minimum = MinValue; } } private void DefaultItems() { var item = new ComboDataItem(3); Items.Add(item); item = new ComboDataItem(6); Items.Add(item); item = new ComboDataItem(9); Items.Add(item); item = new ComboDataItem(12); Items.Add(item); item = new ComboDataItem(15); Items.Add(item); item = new ComboDataItem(18); Items.Add(item); item = new ComboDataItem(21); Items.Add(item); item = new ComboDataItem(24); Items.Add(item); combox.ItemSource = Items; UpdateMaxMinValue(Items); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { ThicknessSlider.ValueChanged += ThicknessSlider_ValueChanged; combox.ValueChanged -= combox_ValueChanged; combox.ValueChanged += combox_ValueChanged; } private void combox_ValueChanged(object sender, RoutedEventArgs e) { if (sender is double == true) { //点击下拉框时触发 if (combox.IsExistInComBox(sender)) { ThicknessSlider.Value = (double)sender; ValueChanged?.Invoke((double)sender, null); } } } private void ThicknessSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs e) { combox.SelectedItems = new ComboDataItem(Value); // ValueChanged?.Invoke(Value, null); } public double Value { get { return (double)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(SlidComboControl), new PropertyMetadata(1.0, SelectedValuePropertyChanged)); private static void SelectedValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var control = d as SlidComboControl; var value = (double)e.NewValue; if (control != null) { control.combox.SelectedItems = new ComboDataItem(value); } } public List ItemsSource { get { return (List)GetValue(ItemsSourceProperty); } set { SetValue(ItemsSourceProperty, value); } } public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(List), typeof(SlidComboControl), new PropertyMetadata(null, SelectedItemsSourcePropertyChanged)); private static void SelectedItemsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var control = d as SlidComboControl; var value = (List)e.NewValue; if (control != null && value != null) { control.SetItemSource(value); } } private void ThicknessSlider_PreviewMouseMove(object sender, MouseEventArgs e) { if(e.LeftButton == MouseButtonState.Pressed) { ValueChanged?.Invoke(ThicknessSlider.Value, null); } } } }