123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- 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;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- namespace GDNXFD.Alert.Config.Controls
- {
- /// <summary>
- /// Interaction logic for MultiSelectComboBox.xaml
- /// </summary>
- public partial class MultiSelectComboBox : UserControl
- {
- private ObservableCollection<Node> _nodeList;
- public MultiSelectComboBox()
- {
- InitializeComponent();
- _nodeList = new ObservableCollection<Node>();
- }
- #region Dependency Properties
- public static readonly DependencyProperty ItemsSourceProperty =
- DependencyProperty.Register("ItemsSource", typeof(Dictionary<string, object>), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(null,
- new PropertyChangedCallback(MultiSelectComboBox.OnItemsSourceChanged)));
- public static readonly DependencyProperty SelectedItemsProperty =
- DependencyProperty.Register("SelectedItems", typeof(Dictionary<string, object>), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(null,
- new PropertyChangedCallback(MultiSelectComboBox.OnSelectedItemsChanged)));
- // public static readonly DependencyProperty SelectedItemsProperty =
- //DependencyProperty.Register("SelectedItems", typeof(Dictionary<string, object>), typeof(MultiSelectComboBox), new UIPropertyMetadata(null));
- public static readonly DependencyProperty TextProperty =
- DependencyProperty.Register("Text", typeof(string), typeof(MultiSelectComboBox), new UIPropertyMetadata(string.Empty));
- public static readonly DependencyProperty DefaultTextProperty =
- DependencyProperty.Register("DefaultText", typeof(string), typeof(MultiSelectComboBox), new UIPropertyMetadata(string.Empty));
- public Dictionary<string, object> ItemsSource
- {
- get { return (Dictionary<string, object>)GetValue(ItemsSourceProperty); }
- set
- {
- SetValue(ItemsSourceProperty, value);
- }
- }
- public Dictionary<string, object> SelectedItems
- {
- get { return (Dictionary<string, object>)GetValue(SelectedItemsProperty); }
- set
- {
- SetValue(SelectedItemsProperty, value);
- }
- }
- public string Text
- {
- get { return (string)GetValue(TextProperty); }
- set { SetValue(TextProperty, value); }
- }
- public string DefaultText
- {
- get { return (string)GetValue(DefaultTextProperty); }
- set { SetValue(DefaultTextProperty, value); }
- }
- #endregion
- #region Events
- private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- MultiSelectComboBox control = (MultiSelectComboBox)d;
- control.DisplayInControl();
- }
- private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- MultiSelectComboBox control = (MultiSelectComboBox)d;
- control.SelectNodes();
- control.SetText();
- }
- private void CheckBox_Click(object sender, RoutedEventArgs e)
- {
- var xxx = SelectedItems;
- CheckBox clickedBox = (CheckBox)sender;
- if (clickedBox.Content == "全部" )
- {
- if (clickedBox.IsChecked.Value)
- {
- foreach (Node node in _nodeList)
- {
- node.IsSelected = true;
- }
- }
- else
- {
- foreach (Node node in _nodeList)
- {
- node.IsSelected = false;
- }
- }
- }
- else
- {
- int _selectedCount = 0;
- foreach (Node s in _nodeList)
- {
- if (s.IsSelected && s.Title != "全部")
- _selectedCount++;
- }
- if (_selectedCount == _nodeList.Count - 1)
- _nodeList.FirstOrDefault(i => i.Title == "全部").IsSelected = true;
- else
- _nodeList.FirstOrDefault(i => i.Title == "全部").IsSelected = false;
- }
- SetSelectedItems();
- SetText();
- if (SelectionChanged != null)
- {
- SelectionChanged(this, e);
- }
- }
- #endregion
- #region Methods
- private void SelectNodes()
- {
- foreach (KeyValuePair<string, object> keyValue in SelectedItems)
- {
- Node node = _nodeList.FirstOrDefault(i => i.Title == keyValue.Key);
- if (node != null)
- node.IsSelected = true;
- }
- if (SelectedItems != null && SelectedItems.Count == ItemsSource.Count)
- {
- Node node = _nodeList.FirstOrDefault(i => i.Title == "全部");
- if (node != null)
- node.IsSelected = true;
- }
- }
- private void SetSelectedItems()
- {
- if (SelectedItems == null)
- SelectedItems = new Dictionary<string, object>();
- SelectedItems.Clear();
- foreach (Node node in _nodeList)
- {
- if (node.IsSelected && node.Title != "全部")
- {
- if (this.ItemsSource.Count > 0)
- SelectedItems.Add(node.Title, this.ItemsSource[node.Title]);
- }
- }
- }
- private void DisplayInControl()
- {
- _nodeList.Clear();
- if (this.ItemsSource.Count > 0)
- _nodeList.Add(new Node("全部"));
- foreach (KeyValuePair<string, object> keyValue in this.ItemsSource)
- {
- Node node = new Node(keyValue.Key);
- _nodeList.Add(node);
- }
- MultiSelectCombo.ItemsSource = _nodeList;
- }
- private void SetText()
- {
- if (this.SelectedItems != null)
- {
- StringBuilder displayText = new StringBuilder();
- foreach (Node s in _nodeList)
- {
- if (s.IsSelected == true && s.Title == "全部")
- {
- displayText = new StringBuilder();
- displayText.Append("全部");
- break;
- }
- else if (s.IsSelected == true && s.Title != "全部")
- {
- displayText.Append(s.Title);
- displayText.Append(',');
- }
- }
- this.Text = displayText.ToString().TrimEnd(new char[] { ',' });
-
- }
- // set DefaultText if nothing else selected
- if (string.IsNullOrEmpty(this.Text))
- {
- this.Text = this.DefaultText;
- }
- this.ToolTip = this.Text;
- }
- #endregion
- #region 自定义事件
- public event EventHandler SelectionChanged;
- #endregion
- }
- public class Node : INotifyPropertyChanged
- {
- private string _title;
- private bool _isSelected;
- #region ctor
- public Node(string title)
- {
- Title = title;
- }
- #endregion
- #region Properties
- public string Title
- {
- get
- {
- return _title;
- }
- set
- {
- _title = value;
- NotifyPropertyChanged("Title");
- }
- }
- public bool IsSelected
- {
- get
- {
- return _isSelected;
- }
- set
- {
- _isSelected = value;
- NotifyPropertyChanged("IsSelected");
- }
- }
- #endregion
- public event PropertyChangedEventHandler PropertyChanged;
- protected void NotifyPropertyChanged(string propertyName)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- }
- }
|