Просмотр исходного кода

复合控件 - 优化下拉框、滑块

chenrongqian 2 лет назад
Родитель
Сommit
eb676ec36f

+ 97 - 39
PDF Office/CustomControl/CompositeControl/CustomComboControl.xaml.cs

@@ -24,15 +24,21 @@ namespace PDF_Office.CustomControl.CompositeControl
 
     public class ComboDataItem
     {
+        //字符串类型的值
         public string ValueStr { get; private set; }
+        //数字类型的值
         public double Value { get; private set; }
+        //下拉框显示的内容
         public string Content { get; private set; }
+        //下拉框显示的内容+单位:限数字值的单位
         public string Unit { get; private set; }
+        //数字类型
         public ComboDataItem(double value, string unitStr = "")
         {
             Content = value + unitStr;
             Value = value;
         }
+        //字符串类型
         public ComboDataItem(string valueStr, string contentStr = "")
         {
             Content = contentStr;
@@ -54,13 +60,18 @@ namespace PDF_Office.CustomControl.CompositeControl
             Items = new List<ComboDataItem>();
             DefaultItems();
         }
+        private void UserControl_Loaded(object sender, RoutedEventArgs e)
+        {
+            comBox.SelectionChanged -= comBox_SelectionChanged;
+            comBox.SelectionChanged += comBox_SelectionChanged;
+        }
+
         //更换集合
         public void SetItemSource(List<ComboDataItem> items)
         {
             SelectedIndex = - 1;//为了触发SelectedIndex
             Items = items;
-            comBox.ItemsSource = Items;
-            SelectedIndex = ((Items == null || Items.Count == 0) ? -1 : 0);
+            ItemSource = Items;
         }
 
         /// <summary>
@@ -87,29 +98,30 @@ namespace PDF_Office.CustomControl.CompositeControl
             Items.Add(item);
             item = new ComboDataItem(24);
             Items.Add(item);
-
-            comBox.ItemsSource = Items;
-            SelectedIndex = ((Items == null || Items.Count == 0) ? -1 : 0);
-        }
-        private void UserControl_Loaded(object sender, RoutedEventArgs e)
-        {
-            comBox.SelectionChanged -= comBox_SelectionChanged;
-            comBox.SelectionChanged += comBox_SelectionChanged;
+            ItemSource = Items;
         }
-
+ 
+        //下拉框选中项
         private void comBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
         {
             var item = comBox.SelectedItem as ComboDataItem;
             if (item != null)
             {
-                Value = item.Value;
-                ValueStr = item.ValueStr;
                 title.Text = item.Content;
+                SelectedIndex = comBox.SelectedIndex;
 
                 if (IsValueContent == false)
-                    ValueChanged?.Invoke(Value, e);
+                {
+                    Value = item.Value;
+                    ValueChanged?.Invoke(Value, null);
+                }
+                    
                 else
-                    ValueChanged?.Invoke(ValueStr, e);
+                {
+                    ValueStr = item.ValueStr;
+                    ValueChanged?.Invoke(ValueStr, null);
+                }
+
             }
 
 
@@ -127,6 +139,7 @@ namespace PDF_Office.CustomControl.CompositeControl
             set { SetValue(ValueStrProperty, value); }
         }
 
+        //下拉框每项的值是否为字符串类型
         public bool IsValueContent
         {
             get { return (bool)GetValue(IsValueContentProperty); }
@@ -145,11 +158,21 @@ namespace PDF_Office.CustomControl.CompositeControl
             set { SetValue(SelectedIndexProperty, value); }
         }
 
+        public ComboDataItem SelectedItems
+        {
+            get { return (ComboDataItem)GetValue(SelectedItemsProperty); }
+            set { SetValue(SelectedItemsProperty, value); }
+        }
+
+        public static readonly DependencyProperty SelectedItemsProperty =
+         DependencyProperty.Register("SelectedItems", typeof(ComboDataItem), typeof(CustomComboControl), new PropertyMetadata(null, SelectedItemsPropertyChanged));
+
+
         public static readonly DependencyProperty ValueProperty =
-           DependencyProperty.Register("Value", typeof(double), typeof(CustomComboControl), new PropertyMetadata(1.0, SelectedValuePropertyChanged));
+           DependencyProperty.Register("Value", typeof(double), typeof(CustomComboControl), new PropertyMetadata(1.0));
 
         public static readonly DependencyProperty ValueStrProperty =
-         DependencyProperty.Register("ValueStr", typeof(string), typeof(CustomComboControl), new PropertyMetadata("", SelectedValueStrPropertyChanged));
+         DependencyProperty.Register("ValueStr", typeof(string), typeof(CustomComboControl), new PropertyMetadata(""));
 
         public static readonly DependencyProperty ItemSourceProperty =
           DependencyProperty.Register("ItemSource", typeof(List<ComboDataItem>), typeof(CustomComboControl), new PropertyMetadata(null, SelectedItemSourcePropertyChanged));
@@ -160,53 +183,88 @@ namespace PDF_Office.CustomControl.CompositeControl
         public static readonly DependencyProperty IsValueContentProperty =
           DependencyProperty.Register("IsValueContent", typeof(bool), typeof(CustomComboControl), new PropertyMetadata(false));
 
-        private static void SelectedValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+        //集合绑定下拉框 SelectedItems触发属性
+        private static void SelectedItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
         {
             var control = d as CustomComboControl;
-            var value = (double)e.NewValue;
-            if (control != null && control.IsValueContent == false)
+            var selectedItems = (ComboDataItem)e.NewValue;
+            if (control != null & selectedItems != null)
             {
-                control.ValueChanged?.Invoke(value, null);
-            }
-        }
+                if(control.comBox.Items != null && control.comBox.Items.Count > 0)
+                {
+                    if(control.IsValueContent)
+                    {
+                        control.ValueStr = selectedItems.ValueStr;
+                    }
+                    else
+                    {
+                        control.Value = selectedItems.Value;
+                    }
+                }
+
+                if(control.comBox.Items == null || control.comBox.Items.Count == 0)
+                {
+                    control.title.Text = selectedItems.Content;
+                }
+                else
+                {
+                    int index = -1;
+                    ComboDataItem temp;
+                    if (control.IsValueContent)
+                        temp = control.Items.FirstOrDefault(inlineItem => inlineItem.ValueStr == selectedItems.ValueStr);
+                   else
+                        temp = control.Items.FirstOrDefault(inlineItem => inlineItem.Value == selectedItems.Value);
+
+                    if (temp != null)
+                        index = control.Items.IndexOf(temp);
+
+
+                    if (index >= 0)
+                    {
+                        control.SelectedIndex = -1;
+                        control.SelectedIndex = index;
+                    }
+                    else
+                        control.title.Text = selectedItems.Content;
+
+                }
 
-        private static void SelectedValueStrPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
-        {
-            var control = d as CustomComboControl;
-            var value = (string)e.NewValue;
-            if (control != null && control.IsValueContent)
-            {
-                control.ValueChanged?.Invoke(value, null);
             }
         }
-       
+
+        //集合绑定下拉框Itemsource触发属性
         private static void SelectedItemSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
         {
             var control = d as CustomComboControl;
-            var value = (List<ComboDataItem>)e.NewValue;
+            var itemsource = (List<ComboDataItem>)e.NewValue;
             if (control != null)
             {
                 control.SelectedIndex = -1;
-                control.comBox.ItemsSource = value;
-                control.Items = value;
-                control.SelectedIndex = ((value == null || value.Count == 0) ? -1 : 0);
+                control.comBox.ItemsSource = itemsource;
+                control.Items = itemsource;
+                control.SelectedIndex = ((itemsource == null || itemsource.Count == 0) ? -1 : 0);
             }
         }
 
+        //选中项触发属性
         private static void SelectedIndexPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
         {
             var control = d as CustomComboControl;
-            var value = (int)e.NewValue;
+            var selectedIndex = (int)e.NewValue;
             if (control != null)
             {
-                if(control.comBox.Items != null && control.comBox.Items.Count > 0)
+                if(control.comBox.Items != null && control.comBox.Items.Count > 0 && selectedIndex != -1)
                 {
-                    control.comBox.SelectedIndex = value;
+                    control.comBox.SelectedIndex = selectedIndex;
+                    if (control.comBox.SelectedItem != null)
+                        control.SelectedItems = (ComboDataItem)control.comBox.SelectedItem;
+                    else
+                        control.SelectedItems = null;
                 }
                 control.UpdateSelectedIndex();
             }
         }
-
+        //选中项后,更新控件选中的显示内容
         public void UpdateSelectedIndex()
         {
             if(SelectedIndex < 0  || comBox.Items == null || comBox.Items.Count <= SelectedIndex)

+ 2 - 17
PDF Office/CustomControl/CompositeControl/SlidComboControl.xaml

@@ -44,23 +44,8 @@
                 </Slider.Resources>
             </Slider>
 
-            <Border Grid.Column="1" BorderBrush="#E2E3E6"  Width="80" Height="32" Margin="0,0,11,0"  BorderThickness="0">
-                <Grid>
-                    <ComboBox Name="ThicknessBox" BorderThickness="1" Padding="10" Background="Transparent" BorderBrush="#FFE2E3E6" 
-                                  MaxDropDownHeight="200"
-                            ItemTemplate="{StaticResource numberData}"
-                              >
-                        <ComboBox.ItemContainerStyle>
-                            <Style TargetType="{x:Type ComboBoxItem}">
-                                <Setter Property="Padding" Value="10 0 0 0"/>
-                            </Style>
-                        </ComboBox.ItemContainerStyle>
-                    </ComboBox>
-                    <TextBox Name="ThicknessText" Background="White" FontFamily="Segoe UI" FontSize="14"  Height="20" Margin="10,0,35,0" IsReadOnly="True"
-                             BorderThickness="0" VerticalAlignment="Center" TextAlignment="Left" >
-                    </TextBox>
-                </Grid>
-            </Border>
+            <local:CustomComboControl x:Name="combox" Grid.Column="1" Width="80" Height="32"  />
+   
         </Grid>
     </Grid>
    

+ 14 - 28
PDF Office/CustomControl/CompositeControl/SlidComboControl.xaml.cs

@@ -36,7 +36,7 @@ namespace PDF_Office.CustomControl.CompositeControl
         public void SetItemSource(List<ComboDataItem> items)
         {
             Items = items;
-            ThicknessBox.ItemsSource = Items;
+            combox.ItemSource = Items;
         }
 
         private void DefaultItems()
@@ -58,48 +58,31 @@ namespace PDF_Office.CustomControl.CompositeControl
             item = new ComboDataItem(24);
             Items.Add(item);
 
-            ThicknessBox.ItemsSource = Items;
+            combox.ItemSource = Items;
         }
 
         private void UserControl_Loaded(object sender, RoutedEventArgs e)
         {
-            ThicknessBox.SelectionChanged -= ThicknessBox_SelectionChanged;
-            ThicknessBox.SelectionChanged += ThicknessBox_SelectionChanged;
             ThicknessSlider.ValueChanged += ThicknessSlider_ValueChanged;
-            ThicknessBox.SelectedIndex = 0;
+            combox.ValueChanged -= combox_ValueChanged;
+            combox.ValueChanged += combox_ValueChanged;
         }
 
-        private void ThicknessSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
+        private void combox_ValueChanged(object sender, RoutedEventArgs e)
         {
-            foreach(var item in ThicknessBox.Items)
+            if (sender is double == true)
             {
-
-                var itemData = item as ComboDataItem;
-                if(itemData != null)
-                {
-                    if(itemData.Value == Value)
-                    {
-                        ThicknessBox.SelectedItem = itemData;
-                        break;
-                    }
-                }
+                if ((double)sender != Value)
+                    Value = (double)sender;
             }
-            ThicknessText.Text = Value.ToString();
         }
 
-        private void ThicknessBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
+        private void ThicknessSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
         {
-            var item = ThicknessBox.SelectedItem as ComboDataItem;
-            if(item != null)
-            {
-                Value = item.Value;
-                ThicknessText.Text = item.Content;
-                ValueChanged?.Invoke(Value, e);
-            }
-           
-
+            combox.SelectedItems = new ComboDataItem(Value);
         }
 
+
         public double Value
         {
             get { return (double)GetValue(ValueProperty); }
@@ -114,7 +97,10 @@ namespace PDF_Office.CustomControl.CompositeControl
             var value = (double)e.NewValue;
             if (control != null)
             {
+
+                control.combox.SelectedItems = new ComboDataItem(value);
                 control.ValueChanged?.Invoke(value, null);
+
             }
         }