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 { //用法: // IsValueContent为ture时,使用ValueStr作为集合某项的值 //IsValueContent为false时,使用Value作为集合某项的值 //SetItemSource(List items)更换集合List //默认:字体集合、IsValueContent为false、Value作为集合某项的值 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; ValueStr = valueStr; } } /// /// CustomComboControl.xaml 的交互逻辑 /// public partial class CustomComboControl : UserControl { public List Items { get; private set; } public event RoutedEventHandler ValueChanged; public CustomComboControl() { InitializeComponent(); Items = new List(); DefaultItems(); } //更换集合 public void SetItemSource(List items) { SelectedIndex = - 1;//为了触发SelectedIndex Items = items; comBox.ItemsSource = Items; SelectedIndex = ((Items == null || Items.Count == 0) ? -1 : 0); } /// /// 默认集合(字体集合) /// private void DefaultItems() { Items.Clear(); SelectedIndex = -1; 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.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; } 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; if (IsValueContent == false) ValueChanged?.Invoke(Value, e); else ValueChanged?.Invoke(ValueStr, e); } } public double Value { 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 ItemSource { get { return (List)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), 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; var value = (double)e.NewValue; if (control != null && control.IsValueContent == false) { control.ValueChanged?.Invoke(value, null); } } 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); } } private static void SelectedItemSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var control = d as CustomComboControl; var value = (List)e.NewValue; if (control != null) { control.SelectedIndex = -1; control.comBox.ItemsSource = value; control.Items = value; control.SelectedIndex = ((value == null || value.Count == 0) ? -1 : 0); } } private static void SelectedIndexPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { 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 void UpdateSelectedIndex() { if(SelectedIndex < 0 || comBox.Items == null || comBox.Items.Count <= SelectedIndex) { title.Text = ""; } else { title.Text = Items[SelectedIndex].Content; } } } }