123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- 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
- {
- /// <summary>
- /// SlidComboControl.xaml 的交互逻辑
- /// </summary>
- public partial class SlidComboControl : UserControl
- {
- public List<ComboDataItem> Items { get;private set; }
- public event RoutedEventHandler ValueChanged;
-
- public SlidComboControl()
- {
- InitializeComponent();
-
- Items = new List<ComboDataItem>();
- DefaultItems();
- }
- public void SetItemSource(List<ComboDataItem> items)
- {
- Items = items;
- combox.ItemSource = Items;
- UpdateMaxMinValue(items);
- }
- private void UpdateMaxMinValue(List<ComboDataItem> 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<double> 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<ComboDataItem> ItemsSource
- {
- get { return (List<ComboDataItem>)GetValue(ItemsSourceProperty); }
- set { SetValue(ItemsSourceProperty, value); }
- }
- public static readonly DependencyProperty ItemsSourceProperty =
- DependencyProperty.Register("ItemsSource", typeof(List<ComboDataItem>), typeof(SlidComboControl), new PropertyMetadata(null, SelectedItemsSourcePropertyChanged));
- private static void SelectedItemsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- var control = d as SlidComboControl;
- var value = (List<ComboDataItem>)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);
- }
- }
- }
- }
|