diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-09-14 16:49:01 +0300 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-09-14 16:49:01 +0300 |
| commit | 0e32cd5fb97c40624d5323cb265d070b2140d2bc (patch) | |
| tree | 816067dbc24cd56fee2b24d113be90e84e593fa1 /Software/Visual_Studio/Tango.SharedUI/Controls | |
| parent | 13afc37417c9281f5fbaacd174fd151d793151eb (diff) | |
| download | Tango-0e32cd5fb97c40624d5323cb265d070b2140d2bc.tar.gz Tango-0e32cd5fb97c40624d5323cb265d070b2140d2bc.zip | |
Refactored SearchComboBox !!!
Diffstat (limited to 'Software/Visual_Studio/Tango.SharedUI/Controls')
| -rw-r--r-- | Software/Visual_Studio/Tango.SharedUI/Controls/SearchComboBox.cs | 112 |
1 files changed, 62 insertions, 50 deletions
diff --git a/Software/Visual_Studio/Tango.SharedUI/Controls/SearchComboBox.cs b/Software/Visual_Studio/Tango.SharedUI/Controls/SearchComboBox.cs index 8fc6483e9..96e377b7c 100644 --- a/Software/Visual_Studio/Tango.SharedUI/Controls/SearchComboBox.cs +++ b/Software/Visual_Studio/Tango.SharedUI/Controls/SearchComboBox.cs @@ -15,16 +15,12 @@ using System.Windows.Threading; namespace Tango.SharedUI.Controls { - [TemplatePart(Name = SearchComboBox.PartEditor, Type = typeof(System.Windows.Controls.TextBox))] public class SearchComboBox : ComboBox { - public const string PartEditor = "Search"; - private TextBox _textBox; private ListBox _listBox; private ICollectionView _view; - #region Properties public bool IsOpened { get { return (bool)GetValue(IsOpenedProperty); } @@ -57,40 +53,20 @@ namespace Tango.SharedUI.Controls public static readonly DependencyProperty ListItemsSourceProperty = DependencyProperty.Register("ListItemsSource", typeof(IEnumerable), typeof(SearchComboBox), new PropertyMetadata(null)); - #endregion - #region Constructors - - static SearchComboBox() - { - DefaultStyleKeyProperty.OverrideMetadata(typeof(SearchComboBox), new FrameworkPropertyMetadata(typeof(SearchComboBox))); - } - #endregion //Constructors - #region Initialization - - /// <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(); - - _textBox = GetTemplateChild(PartEditor) as TextBox; - if(_textBox != null) - { - _textBox.PreviewKeyDown += _textBox_KeyDown; - } + _textBox = GetTemplateChild("txt") as TextBox; _listBox = GetTemplateChild("list") as ListBox; - if(_listBox != null) - { - _listBox.PreviewKeyDown += _listBox_PreviewKeyDown; - _listBox.PreviewMouseLeftButtonUp += _listBox_PreviewMouseLeftButtonUp; - } - } + _textBox.PreviewKeyDown += _textBox_KeyDown; + _listBox.PreviewKeyDown += _listBox_PreviewKeyDown; + + _listBox.PreviewMouseLeftButtonUp += _listBox_PreviewMouseLeftButtonUp; - #endregion + KeyboardNavigation.SetDirectionalNavigation(this, KeyboardNavigationMode.Once); + } - #region Event Handlers private void _listBox_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Return) @@ -104,9 +80,24 @@ namespace Tango.SharedUI.Controls { if (e.Key == Key.Down) { - _listBox.SelectedIndex = 0; - _listBox.Focus(); + if (!String.IsNullOrWhiteSpace(SearchFilter)) + { + _listBox.SelectedIndex = 0; + } + Keyboard.Focus(_listBox); + + if (_listBox.SelectedIndex != -1) + { + var container = _listBox.ItemContainerGenerator.ContainerFromIndex(_listBox.SelectedIndex) as UIElement; + if (container != null) + { + container.Focus(); + Keyboard.Focus(container); + } + } + e.Handled = true; + _listBox.ScrollIntoView(_listBox.SelectedItem); } } @@ -123,23 +114,15 @@ namespace Tango.SharedUI.Controls { if (IsOpened) { + SearchFilter = String.Empty; _listBox.SelectedItem = SelectedItem; - SearchFilter = ""; + _listBox.ScrollIntoView(_listBox.SelectedItem); await Task.Delay(100); _textBox.Focus(); - Keyboard.Focus(_textBox); } } - private void OnFilterChanged() - { - _view?.Refresh(); - } - - #endregion - #region Override - protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue) { base.OnItemsSourceChanged(oldValue, newValue); @@ -155,9 +138,7 @@ namespace Tango.SharedUI.Controls if (x != null) { - var prop = x.GetType().GetProperty(SearchProperty); - - // var prop = x.GetType().GetProperty(SearchProperty, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly); + var prop = x.GetType().GetProperty(SearchProperty, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); if (prop != null) { String propValue = prop.GetValue(x).ToString(); @@ -170,12 +151,43 @@ namespace Tango.SharedUI.Controls } } - protected override void OnSelectionChanged(SelectionChangedEventArgs e) + private void OnFilterChanged() + { + _view?.Refresh(); + + if (_listBox != null && _listBox.Items.Count > 0) + { + _listBox.ScrollIntoView(_listBox.Items[0]); + } + } + + protected override void OnPreviewKeyDown(KeyEventArgs e) { - base.OnSelectionChanged(e); + //base.OnPreviewKeyDown(e); + e.Handled = false; } - #endregion - + protected override void OnPreviewKeyUp(KeyEventArgs e) + { + //base.OnPreviewKeyUp(e); + e.Handled = false; + } + + protected override void OnKeyDown(KeyEventArgs e) + { + //base.OnKeyDown(e); + e.Handled = false; + } + + protected override void OnKeyUp(KeyEventArgs e) + { + //base.OnKeyUp(e); + e.Handled = false; + } + + static SearchComboBox() + { + DefaultStyleKeyProperty.OverrideMetadata(typeof(SearchComboBox), new FrameworkPropertyMetadata(typeof(SearchComboBox))); + } } } |
