|
@@ -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)
|