Browse Source

复合控件 - 优化通用下拉框

chenrongqian 2 years ago
parent
commit
e18b3e4655
1 changed files with 70 additions and 23 deletions
  1. 70 23
      PDF Office/CustomControl/CompositeControl/CustomComboControl.xaml.cs

+ 70 - 23
PDF Office/CustomControl/CompositeControl/CustomComboControl.xaml.cs

@@ -57,8 +57,10 @@ namespace PDF_Office.CustomControl.CompositeControl
         //更换集合
         public void SetItemSource(List<ComboDataItem> items)
         {
+            SelectedIndex = - 1;//为了触发SelectedIndex
             Items = items;
             comBox.ItemsSource = Items;
+            SelectedIndex = ((Items == null || Items.Count == 0) ? -1 : 0);
         }
 
         /// <summary>
@@ -66,6 +68,9 @@ namespace PDF_Office.CustomControl.CompositeControl
         /// </summary>
         private void DefaultItems()
         {
+            Items.Clear();
+            SelectedIndex = -1;
+
             var item = new ComboDataItem(3);
             Items.Add(item);
             item = new ComboDataItem(6);
@@ -84,6 +89,7 @@ namespace PDF_Office.CustomControl.CompositeControl
             Items.Add(item);
 
             comBox.ItemsSource = Items;
+            SelectedIndex = ((Items == null || Items.Count == 0) ? -1 : 0);
         }
         private void UserControl_Loaded(object sender, RoutedEventArgs e)
         {
@@ -114,9 +120,46 @@ namespace PDF_Office.CustomControl.CompositeControl
             get { return (double)GetValue(ValueProperty); }
             set { SetValue(ValueProperty, value); }
         }
+
+        public string ValueStr
+        {
+            get { return (string)GetValue(ValueStrProperty); }
+            set { SetValue(ValueStrProperty, value); }
+        }
+
+        public bool IsValueContent
+        {
+            get { return (bool)GetValue(IsValueContentProperty); }
+            set { SetValue(IsValueContentProperty, value); }
+        }
+
+        public List<ComboDataItem> ItemSource
+        {
+            get { return (List<ComboDataItem>)GetValue(ItemSourceProperty); }
+            set { SetValue(ItemSourceProperty, value); }
+        }
+
+        public int SelectedIndex
+        {
+            get { return (int)GetValue(SelectedIndexProperty); }
+            set { SetValue(SelectedIndexProperty, value); }
+        }
+
         public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(double), typeof(CustomComboControl), new PropertyMetadata(1.0, SelectedValuePropertyChanged));
 
+        public static readonly DependencyProperty ValueStrProperty =
+         DependencyProperty.Register("ValueStr", typeof(string), typeof(CustomComboControl), new PropertyMetadata("", SelectedValueStrPropertyChanged));
+
+        public static readonly DependencyProperty ItemSourceProperty =
+          DependencyProperty.Register("ItemSource", typeof(List<ComboDataItem>), typeof(CustomComboControl), new PropertyMetadata(null, SelectedItemSourcePropertyChanged));
+
+        public static readonly DependencyProperty SelectedIndexProperty =
+        DependencyProperty.Register("SelectedIndex", typeof(int), typeof(CustomComboControl), new PropertyMetadata(-1, SelectedIndexPropertyChanged));
+
+        public static readonly DependencyProperty IsValueContentProperty =
+          DependencyProperty.Register("IsValueContent", typeof(bool), typeof(CustomComboControl), new PropertyMetadata(false));
+
         private static void SelectedValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
         {
             var control = d as CustomComboControl;
@@ -127,49 +170,53 @@ namespace PDF_Office.CustomControl.CompositeControl
             }
         }
 
-        public string ValueStr
-        {
-            get { return (string)GetValue(ValueStrProperty); }
-            set { SetValue(ValueStrProperty, value); }
-        }
-        public static readonly DependencyProperty ValueStrProperty =
-           DependencyProperty.Register("ValueStr", typeof(string), typeof(CustomComboControl), new PropertyMetadata("", SelectedValueStrPropertyChanged));
-
         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);
+                control.ValueChanged?.Invoke(value, null);
             }
         }
-
-        public List<ComboDataItem> ItemSource
-        {
-            get { return (List<ComboDataItem>)GetValue(ItemSourceProperty); }
-            set { SetValue(ItemSourceProperty, value); }
-        }
-        public static readonly DependencyProperty ItemSourceProperty =
-           DependencyProperty.Register("ItemSource", typeof(List<ComboDataItem>), typeof(CustomComboControl), new PropertyMetadata(null, SelectedItemSourcePropertyChanged));
-
+       
         private static void SelectedItemSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
         {
             var control = d as CustomComboControl;
             var value = (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);
             }
         }
 
-        public bool IsValueContent
+        private static void SelectedIndexPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
         {
-            get { return (bool)GetValue(IsValueContentProperty); }
-            set { SetValue(IsValueContentProperty, value); }
+            var control = d as CustomComboControl;
+            var value = (int)e.NewValue;
+            if (control != null)
+            {
+                if(control.comBox.Items != null && control.comBox.Items.Count > 0)
+                {
+                    control.comBox.SelectedIndex = value;
+                }
+                control.UpdateSelectedIndex();
+            }
         }
-        public static readonly DependencyProperty IsValueContentProperty =
-           DependencyProperty.Register("IsValueContent", typeof(bool), typeof(CustomComboControl), new PropertyMetadata(false));
 
+        public void UpdateSelectedIndex()
+        {
+            if(SelectedIndex < 0  || comBox.Items == null || comBox.Items.Count <= SelectedIndex)
+            {
+                title.Text = "";
+            }
+            else
+            {
+                title.Text = Items[SelectedIndex].Content;
+            }
+        }
     }
 }