123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- 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<ComboDataItem> 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; private set; }
- //数字类型的值
- 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();
- }
- }
- /// <summary>
- /// CustomComboControl.xaml 的交互逻辑
- /// </summary>
- public partial class CustomComboControl : UserControl
- {
- public List<ComboDataItem> 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<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;
- ItemSource = Items;
- }
- /// <summary>
- /// 默认集合(字体集合)
- /// </summary>
- 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);
- 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<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 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<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));
- //集合绑定下拉框 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<ComboDataItem>)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)
- 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;
- }
- }
- }
- }
|