using Microsoft.Office.Interop.Word; using System; using System.Collections.Generic; using System.Diagnostics; 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_Master.CustomControl.CompositeControl { //用法: // IsValueContent为ture时,使用ValueStr作为集合某项的值 //IsValueContent为false时,使用Value作为集合某项的值 //SetItemSource(List items)更换集合List //默认:字体集合、IsValueContent为false、Value作为集合某项的值 public class ComboDataItem : Prism.Mvvm.BindableBase { //private bool needFrontTag = false; //public bool NeedFrontTag //{ // get { return needFrontTag; } // set // { // SetProperty(ref needFrontTag, value); // } //} //字符串类型的值 //public string ValueStr { get; set; } private string valueStr; public string ValueStr { get { return valueStr; } set { SetProperty(ref valueStr, value); } } //数字类型的值 public double Value { get; private set; } //public string Content { get; private set; } //下拉框显示的内容 private string _content = ""; public string Content { get { return _content; } private set { SetProperty(ref _content, value); } } //下拉框显示的内容+单位:限数字值的单位 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; //this.NeedFrontTag = needFrontTag; } public void SetContent(string content) { Content = content; } public static explicit operator double(ComboDataItem v) { throw new NotImplementedException(); } } /// /// CustomComboControl.xaml 的交互逻辑 /// public partial class CustomComboControl : UserControl { public List Items { get; private set; } public event RoutedEventHandler ValueChanged; //private string titleStr; //public string Title //{ // get // { // titleStr = title.Text; // return titleStr; // } // set // { // title.Text = titleStr; // } //} public CustomComboControl() { InitializeComponent(); Items = new List(); DefaultItems(); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { comBox.SelectionChanged -= comBox_SelectionChanged; comBox.SelectionChanged += comBox_SelectionChanged; } //更换集合 public void SetItemSource(List items) { SelectedIndex = -1;//为了触发SelectedIndex Items = items; ItemSource = Items; } /// /// 默认集合(字体集合) /// 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); ItemSource = Items; } //下拉框选中项 private void comBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { var item = comBox.SelectedItem as ComboDataItem; if (item != null) { title.Text = item.Content; Trace.WriteLine("comBox_SelectionChanged" + title.Text); if (SelectedIndex != comBox.SelectedIndex) { SelectedIndex = comBox.SelectedIndex; } if (IsValueContent == false) { Value = item.Value; ValueChanged?.Invoke(Value, null); } else { ValueStr = item.ValueStr; ValueChanged?.Invoke(ValueStr, null); } } } //在外部判断值是否存在于下拉框里 public bool IsExistInComBox(object value) { if (value is double) { var item = Items.FirstOrDefault(temp => temp.Value == (double)Value); return (item == null ? false : true); } else if (value is string) { var item = Items.FirstOrDefault(temp => temp.ValueStr == (string)ValueStr); return (item == null ? false : true); } return false; } 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 ComboDataItem SelectedItems { get { return (ComboDataItem)GetValue(SelectedItemsProperty); } set { SetValue(SelectedItemsProperty, value); } } //标题 public string Title { get { return (string)GetValue(TitleProperty); } set { SetValue(TitleProperty, value); } } public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(CustomComboControl), new PropertyMetadata("", TitlePropertyChanged)); private static void TitlePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var control = d as CustomComboControl; var title = (string)e.NewValue; if (control != null) { control.title.Text = title; } } //外部需要下拉框都不选中时,true为不选中,false为正常选中 public bool IsSelectedEmpty { get { return (bool)GetValue(IsSelectedEmptyProperty); } set { SetValue(IsSelectedEmptyProperty, value); } } public static readonly DependencyProperty IsSelectedEmptyProperty = DependencyProperty.Register("IsSelectedEmpty", typeof(bool), typeof(CustomComboControl), new PropertyMetadata(false, IsSelectedEmptyPropertyChanged)); 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)); public static readonly DependencyProperty ValueStrProperty = DependencyProperty.Register("ValueStr", typeof(string), typeof(CustomComboControl), new PropertyMetadata("")); 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)); //集合绑定下拉框 SelectedItems触发属性 private static void IsSelectedEmptyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var control = d as CustomComboControl; var isSelectedEmpty = (bool)e.NewValue; if (control != null) { //弃用:IsSelectedEmpty属性,可用SelectedItems == null来处理下拉框不选中的情况。 //if(isSelectedEmpty) //{ // control.comBox.SelectedItem = null; // control.SelectedIndex = -1; // control.SelectedItems = null; // control.title.Text = ""; //} } } private static void SelectedItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var control = d as CustomComboControl; var selectedItems = (ComboDataItem)e.NewValue; if (control != null /* && control.IsSelectedEmpty == false*/) { if (selectedItems != null) { if (control.comBox.Items != null && control.comBox.Items.Count > 0) { if (control.IsValueContent) { control.ValueStr = selectedItems.ValueStr; } else { control.Value = selectedItems.Value; Trace.WriteLine("SelectedItemsPropertyChangedValue" + control.Value); } control.title.Text = selectedItems.Content; } 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) { //为了改变值时选项跟着改变,但是值并没有时时刻刻改变,且影响其他事件展示注释掉 2023/4/4 //if (control.SelectedIndex != index) //{ // control.SelectedIndex = index; // Trace.WriteLine("SelectedItemsPropertyChanged" + index); //} //control.SelectedIndex = -1; //control.SelectedIndex = index; } else control.title.Text = selectedItems.Content; } } else { control.comBox.SelectedItem = null; control.SelectedIndex = -1; control.SelectedItems = null; control.title.Text = ""; } } } //集合绑定下拉框Itemsource触发属性 private static void SelectedItemSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var control = d as CustomComboControl; var itemsource = (List)e.NewValue; if (control != null /*& control.IsSelectedEmpty == false*/) { control.SelectedIndex = -1; 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 selectedIndex = (int)e.NewValue; if (control != null /*&& control.IsSelectedEmpty == false*/) { //删掉了selectedIndex != -1,设置不选中任何选项的情况 //删掉了control.SelectedItems = null;避免崩溃 if (control.comBox.Items != null && control.comBox.Items.Count > 0 ) { control.comBox.SelectedIndex = selectedIndex; if (control.comBox.SelectedItem != null) { if(control.comBox.SelectedItem is ComboDataItem comboDataItem) { bool isEqual = EqualityComparer.Default.Equals(comboDataItem, control.SelectedItems); if (isEqual==false) { control.SelectedItems = (ComboDataItem)control.comBox.SelectedItem; } } } } control.UpdateSelectedIndex(); } } //选中项后,更新控件选中的显示内容 public void UpdateSelectedIndex() { if (SelectedIndex < 0 || comBox.Items == null || comBox.Items.Count <= SelectedIndex) { title.Text = ""; } else { if (IsSelectedEmpty == false) title.Text = Items[SelectedIndex].Content; } } } }