-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathAutoSuggestBox.cs
More file actions
277 lines (229 loc) · 9.38 KB
/
AutoSuggestBox.cs
File metadata and controls
277 lines (229 loc) · 9.38 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using System.Collections;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows.Media;
namespace MaterialDesignThemes.Wpf;
[TemplatePart(Name = AutoSuggestBoxListPart, Type = typeof(ListBox))]
public class AutoSuggestBox : TextBox
{
private const string AutoSuggestBoxListPart = "PART_AutoSuggestBoxList";
protected ListBox? _autoSuggestBoxList;
#region Dependency Properties
public IEnumerable? Suggestions
{
get => (IEnumerable)GetValue(SuggestionsProperty);
set => SetValue(SuggestionsProperty, value);
}
public static readonly DependencyProperty SuggestionsProperty =
DependencyProperty.Register(nameof(Suggestions), typeof(IEnumerable), typeof(AutoSuggestBox), new PropertyMetadata(null));
public string ValueMember
{
get => (string)GetValue(ValueMemberProperty);
set => SetValue(ValueMemberProperty, value);
}
public static readonly DependencyProperty ValueMemberProperty =
DependencyProperty.Register(nameof(ValueMember), typeof(string), typeof(AutoSuggestBox), new PropertyMetadata(default(string)));
public string DisplayMember
{
get => (string)GetValue(DisplayMemberProperty);
set => SetValue(DisplayMemberProperty, value);
}
public static readonly DependencyProperty DisplayMemberProperty =
DependencyProperty.Register(nameof(DisplayMember), typeof(string), typeof(AutoSuggestBox), new PropertyMetadata(default(string)));
public Brush DropDownBackground
{
get => (Brush)GetValue(DropDownBackgroundProperty);
set => SetValue(DropDownBackgroundProperty, value);
}
public static readonly DependencyProperty DropDownBackgroundProperty =
DependencyProperty.Register(nameof(DropDownBackground), typeof(Brush), typeof(AutoSuggestBox), new PropertyMetadata(default(Brush)));
public DataTemplate ItemTemplate
{
get => (DataTemplate)GetValue(ItemTemplateProperty);
set => SetValue(ItemTemplateProperty, value);
}
public static readonly DependencyProperty ItemTemplateProperty =
DependencyProperty.Register(nameof(ItemTemplate), typeof(DataTemplate), typeof(AutoSuggestBox), new PropertyMetadata(default(DataTemplate)));
public Style ItemContainerStyle
{
get => (Style)GetValue(ItemContainerStyleProperty);
set => SetValue(ItemContainerStyleProperty, value);
}
public static readonly DependencyProperty ItemContainerStyleProperty =
DependencyProperty.Register(nameof(ItemContainerStyle), typeof(Style), typeof(AutoSuggestBox), new PropertyMetadata(default(Style)));
public Elevation DropDownElevation
{
get => (Elevation)GetValue(DropDownElevationProperty);
set => SetValue(DropDownElevationProperty, value);
}
public static readonly DependencyProperty DropDownElevationProperty =
DependencyProperty.Register(nameof(DropDownElevation), typeof(Elevation), typeof(AutoSuggestBox), new PropertyMetadata(default(Elevation)));
public double DropDownMaxHeight
{
get => (double)GetValue(DropDownMaxHeightProperty);
set => SetValue(DropDownMaxHeightProperty, value);
}
public static readonly DependencyProperty DropDownMaxHeightProperty =
DependencyProperty.Register(nameof(DropDownMaxHeight), typeof(double), typeof(AutoSuggestBox), new PropertyMetadata(200.0));
public bool IsSuggestionOpen
{
get => (bool)GetValue(IsSuggestionOpenProperty);
set => SetValue(IsSuggestionOpenProperty, value);
}
public static readonly DependencyProperty IsSuggestionOpenProperty =
DependencyProperty.Register(nameof(IsSuggestionOpen), typeof(bool), typeof(AutoSuggestBox), new PropertyMetadata(default(bool)));
public object SelectedItem
{
get => GetValue(SelectedItemProperty);
set => SetValue(SelectedItemProperty, value);
}
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register(nameof(SelectedItem), typeof(object), typeof(AutoSuggestBox), new PropertyMetadata(default(object)));
public object SelectedValue
{
get => GetValue(SelectedValueProperty);
set => SetValue(SelectedValueProperty, value);
}
public static readonly DependencyProperty SelectedValueProperty =
DependencyProperty.Register(nameof(SelectedValue), typeof(object), typeof(AutoSuggestBox), new PropertyMetadata(default(object)));
public static readonly RoutedEvent SuggestionChosenEvent =
EventManager.RegisterRoutedEvent(
nameof(SuggestionChosen),
RoutingStrategy.Bubble,
typeof(RoutedPropertyChangedEventHandler<object>),
typeof(AutoSuggestBox));
public event RoutedPropertyChangedEventHandler<object> SuggestionChosen
{
add => AddHandler(SuggestionChosenEvent, value);
remove => RemoveHandler(SuggestionChosenEvent, value);
}
#endregion
static AutoSuggestBox() => DefaultStyleKeyProperty.OverrideMetadata(typeof(AutoSuggestBox), new FrameworkPropertyMetadata(typeof(AutoSuggestBox)));
#region Override methods
public override void OnApplyTemplate()
{
if (_autoSuggestBoxList is not null)
{
_autoSuggestBoxList.PreviewMouseDown -= AutoSuggestionListBox_PreviewMouseDown;
}
if (GetTemplateChild(AutoSuggestBoxListPart) is ListBox listBox)
{
_autoSuggestBoxList = listBox;
base.OnApplyTemplate();
listBox.PreviewMouseDown += AutoSuggestionListBox_PreviewMouseDown;
}
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
base.OnPreviewKeyDown(e);
if (_autoSuggestBoxList is null) return;
switch (e.Key)
{
case Key.Down:
IncrementSelection();
break;
case Key.Up:
DecrementSelection();
break;
case Key.Enter:
CommitValueSelection();
break;
case Key.Escape:
CloseAutoSuggestionPopUp();
break;
case Key.Tab:
CommitValueSelection();
break;
default:
return;
}
e.Handled = true;
}
protected override void OnLostFocus(RoutedEventArgs e)
{
base.OnLostFocus(e);
CloseAutoSuggestionPopUp();
}
protected override void OnTextChanged(TextChangedEventArgs e)
{
base.OnTextChanged(e);
if (_autoSuggestBoxList is null)
return;
if ((Text.Length == 0 || _autoSuggestBoxList.Items.Count == 0) && IsSuggestionOpen)
IsSuggestionOpen = false;
else if (Text.Length > 0 && !IsSuggestionOpen && IsFocused && _autoSuggestBoxList.Items.Count > 0)
IsSuggestionOpen = true;
}
#endregion
#region Callback handlers
private void AutoSuggestionListBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (_autoSuggestBoxList is null || e.OriginalSource is not FrameworkElement element)
return;
var selectedItem = element.DataContext;
if (!_autoSuggestBoxList.Items.Contains(selectedItem))
return;
e.Handled = true;
if (!Equals(_autoSuggestBoxList.SelectedItem, selectedItem))
{
void OnSelectionChanged(object s, SelectionChangedEventArgs args)
{
_autoSuggestBoxList.SelectionChanged -= OnSelectionChanged;
CommitValueSelection();
}
_autoSuggestBoxList.SelectionChanged += OnSelectionChanged;
_autoSuggestBoxList.SelectedItem = selectedItem;
}
else
{
_autoSuggestBoxList.SelectedItem = selectedItem;
CommitValueSelection();
}
}
#endregion
#region Methods
private void CloseAutoSuggestionPopUp()
{
IsSuggestionOpen = false;
}
private void CommitValueSelection()
=> CommitValueSelection(_autoSuggestBoxList?.SelectedValue);
private void CommitValueSelection(object? selectedValue)
{
string oldValue = Text;
Text = selectedValue?.ToString();
if (Text != null)
{
CaretIndex = Text.Length;
}
CloseAutoSuggestionPopUp();
var args = new RoutedPropertyChangedEventArgs<object?>(oldValue, Text)
{
RoutedEvent = SuggestionChosenEvent
};
RaiseEvent(args);
}
private void DecrementSelection()
{
if (_autoSuggestBoxList is null || Suggestions is null)
return;
ICollectionView collectionView = CollectionViewSource.GetDefaultView(Suggestions);
if (collectionView.IsCurrentBeforeFirst)
collectionView.MoveCurrentToLast();
else
collectionView.MoveCurrentToPrevious();
_autoSuggestBoxList.ScrollIntoView(_autoSuggestBoxList.SelectedItem);
}
private void IncrementSelection()
{
if (_autoSuggestBoxList is null || Suggestions is null)
return;
ICollectionView collectionView = CollectionViewSource.GetDefaultView(Suggestions);
if (collectionView.IsCurrentAfterLast)
collectionView.MoveCurrentToFirst();
else
collectionView.MoveCurrentToNext();
_autoSuggestBoxList.ScrollIntoView(_autoSuggestBoxList.SelectedItem);
}
#endregion
}