aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.SharedUI/Controls/SearchComboBox.cs
blob: 8fc6483e9c971218fa665bab3c3e50086703ea94 (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
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
{
    [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); }
            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));

        #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;
            }
            _listBox = GetTemplateChild("list") as ListBox;
            if(_listBox != null)
            {
                _listBox.PreviewKeyDown += _listBox_PreviewKeyDown;
                _listBox.PreviewMouseLeftButtonUp += _listBox_PreviewMouseLeftButtonUp;
            }
        }

        #endregion

        #region Event Handlers
        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)
            {
                _listBox.SelectedIndex = 0;
                _listBox.Focus();
                Keyboard.Focus(_listBox);
            }
        }

        private void _listBox_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (!(e.OriginalSource is Thumb))
            {
                IsOpened = false;
                SelectedItem = _listBox.SelectedItem;
            }
        }

        private async void OnIsOpenedChanged()
        {
            if (IsOpened)
            {
                _listBox.SelectedItem = SelectedItem;
                SearchFilter = "";
                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);

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

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

                    return false;
                };
            }
        }

        protected override void OnSelectionChanged(SelectionChangedEventArgs e)
        {
            base.OnSelectionChanged(e);
        }

        #endregion
        
    }
}