1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- 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 event EventHandler<double> ValueChanged;
- public SlidComboControl()
- {
- InitializeComponent();
- }
- private void UserControl_Loaded(object sender, RoutedEventArgs e)
- {
- ThicknessBox.SelectionChanged -= ThicknessBox_SelectionChanged;
- ThicknessBox.SelectionChanged += ThicknessBox_SelectionChanged;
- ThicknessSlider.ValueChanged += ThicknessSlider_ValueChanged;
- ThicknessBox.SelectedIndex = 0;
- }
- private void ThicknessSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
- {
- foreach(var item in ThicknessBox.Items)
- {
- var itemData = item as ComboBoxItem;
- if(itemData != null)
- {
- var doubleItem = double.Parse(itemData.Content.ToString());
- if(doubleItem == Value)
- {
- ThicknessBox.SelectedItem = itemData;
- break;
- }
- }
- }
- ThicknessText.Text = Value.ToString();
- }
- private void ThicknessBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- var listItem = ThicknessBox.ItemContainerGenerator.ContainerFromItem(ThicknessBox.SelectedItem) as ComboBoxItem;
- if (listItem != null)
- {
- Value = double.Parse(listItem.Content.ToString());
- ThicknessText.Text = listItem.Content.ToString();
- }
- else
- {
- ThicknessText.Text = "1";
- Value = 1.0;
- }
-
-
- }
- 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.ValueChanged?.Invoke(control, value);
- }
- }
- }
- }
|