aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.SharedUI/Controls/SearchComboBox.cs
blob: d7439f702ec7c1c33c14a79ad82b022de85ab680 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Linq;
using System.Windows.Media;
using System.Windows.Threading;

namespace Tango.SharedUI.Controls
{
    public class SearchComboBox : ComboBox
    {
        private TextBox _textBox;
        private ListBox _listBox;
        private ICollectionView _view;

        public bool IsOpened
        {
            get { return (bool)GetValue(IsOpenedProperty); }
            set { SetValue(IsOpenedProperty, value); }
        }
        public static readonly DependencyProperty IsOpenedProperty =
            DependencyProperty.Register("IsOpened", typeof(bool), typeof(SearchComboBox), new PropertyMetadata(false, (d, e) => (d as SearchComboBox).OnIsOpenedChanged()));

        public String SearchProperty
        {
            get { return (String)GetValue(SearchPropertyProperty); }
            set { SetValue(SearchPropertyProperty, value); }
        }
        public static readonly DependencyProperty SearchPropertyProperty =
            DependencyProperty.Register("SearchProperty", typeof(String), typeof(SearchComboBox), new PropertyMetadata(null));

        public String SearchFilter
        {
            get { return (String)GetValue(SearchFilterProperty); }
            set { SetValue(SearchFilterProperty, value); }
        }
        public static readonly DependencyProperty SearchFilterProperty =
            DependencyProperty.Register("SearchFilter", typeof(String), typeof(SearchComboBox), new PropertyMetadata(null, (d, e) => (d as SearchComboBox).OnFilterChanged()));

        public IEnumerable ListItemsSource
        {
            get { return (IEnumerable)GetValue(ListItemsSourceProperty); }
            set { SetValue(ListItemsSourceProperty, value); }
        }
        public static readonly DependencyProperty ListItemsSourceProperty =
            DependencyProperty.Register("ListItemsSource", typeof(IEnumerable), typeof(SearchComboBox), new PropertyMetadata(null));


        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _textBox = GetTemplateChild("txt") as TextBox;
            _listBox = GetTemplateChild("list") as ListBox;
            _textBox.PreviewKeyDown += _textBox_KeyDown;
            _listBox.PreviewKeyDown += _listBox_PreviewKeyDown;

            _listBox.PreviewMouseLeftButtonUp += _listBox_PreviewMouseLeftButtonUp;

            KeyboardNavigation.SetDirectionalNavigation(this, KeyboardNavigationMode.Once);
        }

        private void _listBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                IsOpened = false;
                SelectedItem = _listBox.SelectedItem;
            }
        }

        private void _textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Down)
            {
                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);
            }
        }

        private void _listBox_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (_listBox.SelectedItem != null && (e.OriginalSource as FrameworkElement != null) && (e.OriginalSource as FrameworkElement).DataContext == _listBox.SelectedItem)
            {
                IsOpened = false;
                SelectedItem = _listBox.SelectedItem;
            }
        }

        private async void OnIsOpenedChanged()
        {
            if (IsOpened)
            {
                SearchFilter = String.Empty;
                _listBox.SelectedItem = SelectedItem;
                _listBox.ScrollIntoView(_listBox.SelectedItem);
                await Task.Delay(100);
                _textBox.Focus();
                Keyboard.Focus(_textBox);
            }
        }

        protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
        {
            base.OnItemsSourceChanged(oldValue, newValue);

            if (ItemsSource != null)
            {
                ListItemsSource = ItemsSource.Cast<Object>().ToList();

                _view = CollectionViewSource.GetDefaultView(ListItemsSource);
                _view.Filter = (x) =>
                {
                    if (String.IsNullOrWhiteSpace(SearchFilter) || SearchFilter == null) return true;

                    if (x != null)
                    {
                        var prop = x.GetType().GetProperty(SearchProperty, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
                        if (prop != null)
                        {
                            String propValue = prop.GetValue(x).ToString();
                            return propValue.ToLower().Contains(SearchFilter.ToLower());
                        }
                    }

                    return false;
                };
            }
        }

        private void OnFilterChanged()
        {
            _view?.Refresh();

            if (_listBox != null && _listBox.Items.Count > 0)
            {
                _listBox.ScrollIntoView(_listBox.Items[0]);
            }
        }

        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            //base.OnPreviewKeyDown(e);
            e.Handled = false;
        }

        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)));
        }
    }
}