| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- using System;
- using System.ComponentModel;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Data;
- using System.Collections.ObjectModel;
- using System.Windows.Input;
- using SHJX.Service.Library.Common;
- namespace CustomUI
- {
- /// <summary>
- /// 多选下拉框
- /// </summary>
- public class CheckComboBox : Selector
- {
- #region private fields
- private ContentPresenter PART_ContentSite;
- private TextBox PART_FilterTextBox;
- private ICollectionView view;
- private Popup PART_Popup;
- private bool mPopupIsFirstOpen;
- #endregion
- #region DependencyProperty
- #region Content
- public string Content
- {
- get { return (string)GetValue(ContentProperty); }
- set { SetValue(ContentProperty, value); }
- }
- public static readonly DependencyProperty ContentProperty =
- DependencyProperty.Register("Content", typeof(string), typeof(CheckComboBox), new PropertyMetadata(string.Empty));
- #endregion
- #region Value
- public string Value
- {
- get { return (string)GetValue(ValueProperty); }
- set { SetValue(ValueProperty, value); }
- }
- public static readonly DependencyProperty ValueProperty =
- DependencyProperty.Register("Value", typeof(string), typeof(CheckComboBox), new PropertyMetadata(string.Empty));
- #endregion
- #region SelectedObjList
- public ObservableCollection<object> SelectedObjList
- {
- get { return (ObservableCollection<object>)GetValue(SelectedObjListProperty); }
- set { SetValue(SelectedObjListProperty, value); }
- }
- public static readonly DependencyProperty SelectedObjListProperty =
- DependencyProperty.Register("SelectedObjList", typeof(ObservableCollection<object>), typeof(CheckComboBox), new PropertyMetadata(null));
- #endregion
- #region SelectedStrList
- public ObservableCollection<string> SelectedStrList
- {
- get { return (ObservableCollection<string>)GetValue(SelectedStrListProperty); }
- set { SetValue(SelectedStrListProperty, value); }
- }
- public static readonly DependencyProperty SelectedStrListProperty =
- DependencyProperty.Register("SelectedStrList", typeof(ObservableCollection<string>), typeof(CheckComboBox));
- #endregion
- #region IsDropDownOpen
- public bool IsDropDownOpen
- {
- get { return (bool)GetValue(IsDropDownOpenProperty); }
- set { SetValue(IsDropDownOpenProperty, value); }
- }
- public static readonly DependencyProperty IsDropDownOpenProperty =
- DependencyProperty.Register("IsDropDownOpen", typeof(bool), typeof(CheckComboBox), new PropertyMetadata(false, OnIsDropDownOpenChanged));
- private static void OnIsDropDownOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- CheckComboBox checkComboBox = d as CheckComboBox;
- }
- #endregion
- #region IsShowFilterBox
- /// <summary>
- /// 获取或者设置下拉列表过滤文本框的显示与隐藏
- /// </summary>
- public bool IsShowFilterBox
- {
- get { return (bool)GetValue(IsShowFilterBoxProperty); }
- set { SetValue(IsShowFilterBoxProperty, value); }
- }
- public static readonly DependencyProperty IsShowFilterBoxProperty =
- DependencyProperty.Register("IsShowFilterBox", typeof(bool), typeof(CheckComboBox), new PropertyMetadata(false));
- #endregion
- #region MaxShowNumber
- /// <summary>
- /// 获取或者设置最多显示的选中个数
- /// </summary>
- public int MaxShowNumber
- {
- get { return (int)GetValue(MaxShowNumberProperty); }
- set { SetValue(MaxShowNumberProperty, value); }
- }
- public static readonly DependencyProperty MaxShowNumberProperty =
- DependencyProperty.Register("MaxShowNumber", typeof(int), typeof(CheckComboBox), new PropertyMetadata(4));
- #endregion
- #region MaxDropDownHeight
- public double MaxDropDownHeight
- {
- get { return (double)GetValue(MaxDropDownHeightProperty); }
- set { SetValue(MaxDropDownHeightProperty, value); }
- }
- public static readonly DependencyProperty MaxDropDownHeightProperty =
- DependencyProperty.Register("MaxDropDownHeight", typeof(double), typeof(CheckComboBox), new PropertyMetadata(200d));
- #endregion
- #region FilterBoxWatermark
- public string FilterBoxWatermark
- {
- get { return (string)GetValue(FilterBoxWatermarkProperty); }
- set { SetValue(FilterBoxWatermarkProperty, value); }
- }
- public static readonly DependencyProperty FilterBoxWatermarkProperty =
- DependencyProperty.Register("FilterBoxWatermark", typeof(string), typeof(CheckComboBox), new PropertyMetadata("Enter keyword filtering"));
- #endregion
- #endregion
- private bool HasCapture
- {
- get
- {
- return Mouse.Captured == this;
- }
- }
- #region Constructors
- static CheckComboBox()
- {
- DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckComboBox), new FrameworkPropertyMetadata(typeof(CheckComboBox)));
- }
- public CheckComboBox()
- {
- this.SelectedObjList = new ObservableCollection<object>();
- this.SelectedStrList = new ObservableCollection<string>();
- }
- #endregion
- #region Override
- public override void OnApplyTemplate()
- {
- base.OnApplyTemplate();
- if (this.PART_FilterTextBox != null)
- {
- this.PART_FilterTextBox.TextChanged -= PART_FilterTextBox_TextChanged;
- }
- if (PART_Popup != null)
- {
- this.PART_Popup.Opened -= PART_Popup_Opened;
- }
- this.PART_ContentSite = this.GetTemplateChild("PART_ContentSite") as ContentPresenter;
- this.PART_FilterTextBox = this.GetTemplateChild("PART_FilterTextBox") as TextBox;
- this.PART_Popup = this.GetTemplateChild("PART_Popup") as Popup;
- if (this.PART_FilterTextBox != null)
- {
- this.PART_FilterTextBox.TextChanged += PART_FilterTextBox_TextChanged;
- }
- view = CollectionViewSource.GetDefaultView(this.ItemsSource);
- if (PART_Popup != null)
- {
- this.PART_Popup.Opened += PART_Popup_Opened;
- }
- this.Init();
- }
- protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
- {
- if (!(item is CheckComboBoxItem))
- {
- CheckComboBoxItem checkComboBoxItem = element as CheckComboBoxItem;
- if (checkComboBoxItem != null && !string.IsNullOrEmpty(this.DisplayMemberPath))
- {
- Binding binding = new Binding(this.DisplayMemberPath);
- checkComboBoxItem.SetBinding(CheckComboBoxItem.ContentProperty, binding);
- }
- }
- base.PrepareContainerForItemOverride(element, item);
- }
- protected override DependencyObject GetContainerForItemOverride()
- {
- return new CheckComboBoxItem();
- }
- protected override bool IsItemItsOwnContainerOverride(object item)
- {
- return (item is CheckComboBoxItem);
- }
- #endregion
- #region private function
- private void Init()
- {
- this.mPopupIsFirstOpen = true;
- if (this.SelectedObjList != null)
- {
- foreach (var obj in this.SelectedObjList)
- {
- if (string.IsNullOrWhiteSpace(this.DisplayMemberPath))
- {
- this.SelectedStrList.Add(obj.ToString());
- }
- else
- {
- this.SelectedStrList.Add(CommonUtil.GetPropertyValue(obj, this.DisplayMemberPath).ToString());
- }
- }
- }
- this.SetCheckComboBoxValueAndContent();
- }
- public void SetCheckComboBoxValueAndContent()
- {
- if (this.SelectedStrList == null) return;
- if (this.SelectedStrList.Count > this.MaxShowNumber)
- {
- this.Content = this.SelectedStrList.Count + " 个被选中";
- }
- else
- {
- this.Content = this.SelectedStrList.Aggregate("", (current, p) => current + (p + ", ")).TrimEnd(new char[] { ' ' }).TrimEnd(new char[] { ',' });
- }
- this.Value = this.SelectedStrList.Aggregate("", (current, p) => current + (p + ",")).TrimEnd(new char[] { ',' });
- }
- #endregion
- #region internal
- /// <summary>
- /// 行选中
- /// </summary>
- /// <param name="item"></param>
- internal void NotifyCheckComboBoxItemClicked(CheckComboBoxItem item)
- {
- item.SetValue(CheckComboBoxItem.IsSelectedProperty, !item.IsSelected);
- string itemContent = Convert.ToString(item.Content);
- if (item.IsSelected)
- {
- if (!this.SelectedStrList.Contains(item.Content))
- {
- this.SelectedStrList.Add(itemContent);
- }
- if (!this.SelectedObjList.Contains(item.DataContext))
- {
- this.SelectedObjList.Add(item.DataContext);
- }
- }
- else
- {
- if (this.SelectedStrList.Contains(itemContent))
- {
- this.SelectedStrList.Remove(itemContent);
- }
- if (this.SelectedObjList.Contains(item.DataContext))
- {
- this.SelectedObjList.Remove(item.DataContext);
- }
- }
- this.SetCheckComboBoxValueAndContent();
- }
- #endregion
- #region Event Implement Function
- /// <summary>
- /// 每次Open回显数据不太好,先这么处理
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void PART_Popup_Opened(object sender, EventArgs e)
- {
- if (!this.mPopupIsFirstOpen) return;
- this.mPopupIsFirstOpen = false;
- if (this.ItemsSource == null || this.SelectedObjList == null) return;
- foreach (var obj in this.SelectedObjList)
- {
- foreach (var item in this.ItemsSource)
- {
- if (item == obj)
- {
- if (ItemContainerGenerator.ContainerFromItem(item) is CheckComboBoxItem checkComboBoxItem)
- {
- checkComboBoxItem.IsSelected = true;
- break;
- }
- }
- }
- }
- }
- private void PART_FilterTextBox_TextChanged(object sender, TextChangedEventArgs e)
- {
- if (this.PART_FilterTextBox == null || view == null) return;
- view.Filter += (o) =>
- {
- string value = Convert.ToString(CommonUtil.GetPropertyValue(o, this.DisplayMemberPath)).ToLower();
- return value.IndexOf(this.PART_FilterTextBox.Text.ToLower()) != -1;
- };
- }
- #endregion
- }
- }
|