diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-04-21 01:07:20 +0300 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-04-21 01:07:20 +0300 |
| commit | 97a784b6ce43960bdb92465b08f26d3562a4f202 (patch) | |
| tree | f065b36478862b09960f02a46afcc43b4c0131da /Software/Visual_Studio/Tango.SharedUI/Controls | |
| parent | 1b00bc62f17c4d8e308f99a522937c5cefbeab7a (diff) | |
| parent | 5677c3d08e15bef1d6629de42b6c2749f4efcef0 (diff) | |
| download | Tango-97a784b6ce43960bdb92465b08f26d3562a4f202.tar.gz Tango-97a784b6ce43960bdb92465b08f26d3562a4f202.zip | |
MERGE!
Diffstat (limited to 'Software/Visual_Studio/Tango.SharedUI/Controls')
| -rw-r--r-- | Software/Visual_Studio/Tango.SharedUI/Controls/MultiSelectComboBox.cs | 272 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.SharedUI/Controls/MultiSelectComboBoxItem.cs | 39 |
2 files changed, 257 insertions, 54 deletions
diff --git a/Software/Visual_Studio/Tango.SharedUI/Controls/MultiSelectComboBox.cs b/Software/Visual_Studio/Tango.SharedUI/Controls/MultiSelectComboBox.cs index 66eaa4e14..153b36804 100644 --- a/Software/Visual_Studio/Tango.SharedUI/Controls/MultiSelectComboBox.cs +++ b/Software/Visual_Studio/Tango.SharedUI/Controls/MultiSelectComboBox.cs @@ -16,35 +16,90 @@ using System.Windows.Navigation; using System.Windows.Shapes; using Tango.Core.Commands; using System.ComponentModel; +using System.Windows.Controls.Primitives; +using System.Windows.Threading; +using System.Diagnostics; namespace Tango.SharedUI.Controls { + internal sealed class MultiSelectComboBoxTemplateSelector : DataTemplateSelector + { + public override DataTemplate SelectTemplate(object item, DependencyObject container) + { + FrameworkElement element = container as FrameworkElement; + if (element != null && item != null && item is MultiSelectComboBoxItem) + return element.FindResource("CheckItem") as DataTemplate; + + return element.FindResource("SearchItem") as DataTemplate; ; + + } + } + + [TemplatePart(Name = MultiSelectComboBox.PartEditor, Type = typeof(TextBox))] + [TemplatePart(Name = MultiSelectComboBox.PartPopup, Type = typeof(Popup))] public class MultiSelectComboBox : Control { + #region Fields + public const string PartEditor = "Edit_PART"; + public const string PartPopup = "MultiSel_Popup"; + private ItemsControl _items_control; + private bool _isLoaded; + #endregion + #region "Constructors" + static MultiSelectComboBox() { DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(typeof(MultiSelectComboBox))); } - public MultiSelectComboBox() : base() { - RemoveAllCommand = new RelayCommand((() => SelectedItemsList.Clear()), () => SelectedItemsList != null && SelectedItemsList.Count > 0); - RemoveItemCommand = new RelayCommand<string>((item) => SelectedItemsList.Remove(item), () => SelectedItemsList != null); - AddItemCommand = new RelayCommand<string>((item) => SelectedItemsList.Add(item)); - SearchText = ""; + SelectedItemsList = new ObservableCollection<object>(); + RemoveAllCommand = new RelayCommand(RemoveAllSelectedItems, CanRemoveAllSelectedItems); + RemoveItemCommand = new RelayCommand<MultiSelectComboBoxItem>(RemoveSelectedItem); + AddItemCommand = new RelayCommand<string>(AddSelectedItem); + SearchText = new MultiSelectComboBoxSearchItem(); + Loaded += MultiSelectComboBox_Loaded; + _isLoaded = false; + + } + #endregion #region Properties - public ObservableCollection<string> SelectedItemsList + private TextBox _editor; + + public TextBox Editor { - get { return (ObservableCollection<string>)GetValue(SelectedItemsListProperty); } + get { return _editor; } + set { _editor = value; } + } + + private Popup _popup; + + public Popup Popup + { + get { return _popup; } + set { _popup = value; } + } + + private DispatcherTimer _fetchTimer; + + public DispatcherTimer FetchTimer + { + get { return _fetchTimer; } + set { _fetchTimer = value; } + } + + public ObservableCollection<object> SelectedItemsList + { + get { return (ObservableCollection<object>)GetValue(SelectedItemsListProperty); } set { SetValue(SelectedItemsListProperty, value); } } public static readonly DependencyProperty SelectedItemsListProperty = - DependencyProperty.Register("SelectedItemsList", typeof(ObservableCollection<string>), typeof(MultiSelectComboBox), new PropertyMetadata(default(ObservableCollection<string>))); + DependencyProperty.Register("SelectedItemsList", typeof(ObservableCollection<object>), typeof(MultiSelectComboBox), new PropertyMetadata(default(ObservableCollection<object>))); public ListCollectionView SearchItemsList @@ -67,114 +122,223 @@ namespace Tango.SharedUI.Controls public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(ObservableCollection<string>), typeof(MultiSelectComboBox), new PropertyMetadata(default(ObservableCollection<string>))); - - + public bool IsToggleChecked { get { return (bool)GetValue(IsToggleCheckedProperty); } set { SetValue(IsToggleCheckedProperty, value); } } - public static readonly DependencyProperty IsToggleCheckedProperty = - DependencyProperty.Register("IsToggleChecked", typeof(bool), typeof(MultiSelectComboBox), new PropertyMetadata(false)); - + public static readonly DependencyProperty IsToggleCheckedProperty = + DependencyProperty.Register("IsToggleChecked", typeof(bool), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(false)); - private string _searchText; + private MultiSelectComboBoxSearchItem _searchText; /// <summary> /// Gets or sets the search text of TextBox. /// </summary> - public string SearchText + public MultiSelectComboBoxSearchItem SearchText { get { return _searchText; } - set {_searchText = value; - RaisePropertyChanged("SearchText"); + set + { + _searchText = value; OnTextChanged(); } } + #endregion + + private void MultiSelectComboBox_Loaded(object sender, RoutedEventArgs e) + { + if (!_isLoaded) + { + ToggleButton b2 = GetTemplateChild("MultiSelToggleButton") as ToggleButton; + if (b2 != null) + { + // ct.LoadContent(); + _items_control = b2.Template.FindName("SelectedItemsControl", b2) as ItemsControl; + if (_items_control != null) + { + var wrapPanel = _items_control.FindChild<WrapPanel>(); + if (wrapPanel != null) + { + _items_control.Bind(ItemsControl.HeightProperty, wrapPanel, WrapPanel.ActualHeightProperty, BindingMode.OneWay); + _isLoaded = true; + + var searchItem =(_items_control.ItemContainerGenerator.ContainerFromItem(SearchText)); + Editor = searchItem.FindVisualChildren< TextBox>().FirstOrDefault(x => x.Name == PartEditor); + if (Editor != null) + { + Editor.TextChanged += OnEditorTextChanged; + Editor.PreviewKeyDown += OnEditorKeyDown; + Editor.LostFocus += OnEditorLostFocus; + Editor.GotFocus += OnEditorGotFocus; + } + } + } + this.GotFocus += SelTextBox_GotFocus; + } + } + } void OnTextChanged() { if(SearchItemsList != null) { - if (String.IsNullOrEmpty(SearchText)) + if (String.IsNullOrEmpty(SearchText.Text)) SearchItemsList.Filter = null; else - SearchItemsList.Filter = new Predicate<object>(o => ((string)o == SearchText)); + SearchItemsList.Filter = new Predicate<object>(o => (((string)o).ToLower().Contains(SearchText.Text.ToLower()))); } } /// <summary> - /// Occurs when a property has changed. - /// </summary> - public event PropertyChangedEventHandler PropertyChanged; - - /// <summary> - /// Raises the property changed event. - /// </summary> - /// <param name="propName">Name of the property.</param> - protected virtual void RaisePropertyChanged(String propName) - { - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName)); - } - - - #endregion - - /// <summary> /// When overridden in a derived class, is invoked whenever application code or internal processes call <see cref="M:System.Windows.FrameworkElement.ApplyTemplate" />. /// </summary> public override void OnApplyTemplate() { base.OnApplyTemplate(); if (SelectedItemsList == null) - SelectedItemsList = new ObservableCollection<string>(); + SelectedItemsList = new ObservableCollection<object>(); + SelectedItemsList.Add(SearchText); + SearchItemsList = new ListCollectionView(this.Items); + Popup = Template.FindName(PartPopup, this) as Popup; + - //ItemsControl iControl = FindName("asd") as ItemsControl; - //iControl.ItemContainerGenerator.ItemsChanged += ItemContainerGenerator_ItemsChanged; - //iControl.ItemContainerGenerator.ContainerFromItem(null); + if (Popup != null) + { + Popup.StaysOpen = false; + Popup.Opened += OnPopupOpened; + Popup.Closed += OnPopupClosed; + } + } + + private void OnEditorTextChanged(object sender, TextChangedEventArgs e) + { + if (FetchTimer == null) + { + FetchTimer = new DispatcherTimer(); + FetchTimer.Interval = TimeSpan.FromMilliseconds(500); + FetchTimer.Tick += OnFetchTimerTick; + } + FetchTimer.IsEnabled = false; + FetchTimer.Stop(); + IsToggleChecked = true; + + FetchTimer.IsEnabled = true; + FetchTimer.Start(); + + } + + private void OnFetchTimerTick(object sender, EventArgs e) + { + FetchTimer.IsEnabled = false; + FetchTimer.Stop(); + SearchText.Text = Editor.Text; + OnTextChanged(); + } - //for (int i = 0; i < iControl.Items.Count; i++) - //{ - // FrameworkElement a = iControl.ItemContainerGenerator.ContainerFromIndex(i) as FrameworkElement; - //} + private void OnEditorKeyDown(object sender, KeyEventArgs e) + { + IsToggleChecked = true; } - private void ItemContainerGenerator_ItemsChanged(object sender, System.Windows.Controls.Primitives.ItemsChangedEventArgs e) + private void OnEditorLostFocus(object sender, RoutedEventArgs e) + { + if (!IsKeyboardFocusWithin) + { + IsToggleChecked = false; + } + } + + private void SelTextBox_GotFocus(object sender, RoutedEventArgs e) + { + Editor?.Focus(); + } + + private void OnEditorGotFocus(object sender, RoutedEventArgs e) + { + IsToggleChecked = true; + } + + private void OnPopupOpened(object sender, EventArgs e) { } + private void OnPopupClosed(object sender, EventArgs e) + { + IsToggleChecked = false; + } #region Commands /// <summary> /// The remove all command property for clear SelectedItemsList /// </summary> - public static DependencyProperty RemoveAllCommandProperty = DependencyProperty.Register("RemoveAllCommand", typeof(ICommand), typeof(MultiSelectComboBox)); - public ICommand RemoveAllCommand + public static DependencyProperty RemoveAllCommandProperty = DependencyProperty.Register("RemoveAllCommand", typeof(RelayCommand), typeof(MultiSelectComboBox)); + public RelayCommand RemoveAllCommand { - get { return (ICommand)GetValue(RemoveAllCommandProperty); } + get { return (RelayCommand)GetValue(RemoveAllCommandProperty); } private set { SetValue(RemoveAllCommandProperty, value); } } /// <summary> /// The remove item from SelectedItemsList command property /// </summary> - public static DependencyProperty RemoveItemCommandProperty = DependencyProperty.Register("RemoveItemCommand", typeof(RelayCommand<string>), typeof(MultiSelectComboBox)); - public RelayCommand<string> RemoveItemCommand + public static DependencyProperty RemoveItemCommandProperty = DependencyProperty.Register("RemoveItemCommand", typeof(RelayCommand<MultiSelectComboBoxItem>), typeof(MultiSelectComboBox)); + public RelayCommand<MultiSelectComboBoxItem> RemoveItemCommand { - get { return (RelayCommand<string>)GetValue(RemoveItemCommandProperty); } + get { return (RelayCommand<MultiSelectComboBoxItem>)GetValue(RemoveItemCommandProperty); } private set { SetValue(RemoveItemCommandProperty, value); } } - + /// <summary> + /// The add item to SelectedItemsList command property + /// </summary> public static DependencyProperty AddItemCommandProperty = DependencyProperty.Register("AddItemCommand", typeof(RelayCommand<string>), typeof(MultiSelectComboBox)); public RelayCommand<string> AddItemCommand { get { return (RelayCommand<string>)GetValue(AddItemCommandProperty); } private set { SetValue(AddItemCommandProperty, value); } } + + /// <summary> + /// Removes the selected item. + /// </summary> + /// <param name="item">The item.</param> + private void RemoveSelectedItem(IMultiSelectComboBoxItem item) + { + SelectedItemsList.Remove(item); + RemoveAllCommand.RaiseCanExecuteChanged(); + } + + /// <summary> + /// Adds the selected item. + /// </summary> + /// <param name="item">The item.</param> + private void AddSelectedItem(string item) + { + if (SelectedItemsList.Any(x => (x as IMultiSelectComboBoxItem).Text == item)) + return; + SelectedItemsList.Insert(SelectedItemsList.Count - 1, new MultiSelectComboBoxItem(item)); + RemoveAllCommand.RaiseCanExecuteChanged(); + } + /// <summary> + /// Removes all selected items. + /// </summary> + private void RemoveAllSelectedItems() + { + int index = SelectedItemsList.Count - 2; + while (index >= 0) + { + SelectedItemsList.RemoveAt(index--); + } + RemoveAllCommand.RaiseCanExecuteChanged(); + } + private bool CanRemoveAllSelectedItems() + { + return (SelectedItemsList != null && SelectedItemsList.Count > 1); + } #endregion - - + } } diff --git a/Software/Visual_Studio/Tango.SharedUI/Controls/MultiSelectComboBoxItem.cs b/Software/Visual_Studio/Tango.SharedUI/Controls/MultiSelectComboBoxItem.cs new file mode 100644 index 000000000..2dcaa977a --- /dev/null +++ b/Software/Visual_Studio/Tango.SharedUI/Controls/MultiSelectComboBoxItem.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.SharedUI.Controls +{ + interface IMultiSelectComboBoxItem + { + string Text { get; set; } + } + + public class MultiSelectComboBoxItem : IMultiSelectComboBoxItem + { + public string Text { get; set; } + public MultiSelectComboBoxItem(string text) + { + Text = text; + } + public override string ToString() + { + return Text; + } + } + + public class MultiSelectComboBoxSearchItem : IMultiSelectComboBoxItem + { + public string Text { get; set; } + public MultiSelectComboBoxSearchItem(string text = "") + { + Text = text; + } + public override string ToString() + { + return Text; + } + } +} |
