This repository was archived by the owner on Feb 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathTokenizingTextBox.Properties.cs
More file actions
371 lines (329 loc) · 14.2 KB
/
TokenizingTextBox.Properties.cs
File metadata and controls
371 lines (329 loc) · 14.2 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace Microsoft.Toolkit.Uwp.UI.Controls
{
/// <summary>
/// A text input control that auto-suggests and displays token items.
/// </summary>
public partial class TokenizingTextBox : ListViewBase
{
/// <summary>
/// Identifies the <see cref="AutoSuggestBoxStyle"/> property.
/// </summary>
public static readonly DependencyProperty AutoSuggestBoxStyleProperty = DependencyProperty.Register(
nameof(AutoSuggestBoxStyle),
typeof(Style),
typeof(TokenizingTextBox),
new PropertyMetadata(null));
/// <summary>
/// Identifies the <see cref="AutoSuggestBoxTextBoxStyle"/> property.
/// </summary>
public static readonly DependencyProperty AutoSuggestBoxTextBoxStyleProperty = DependencyProperty.Register(
nameof(AutoSuggestBoxTextBoxStyle),
typeof(Style),
typeof(TokenizingTextBox),
new PropertyMetadata(null));
/// <summary>
/// Identifies the <see cref="TextMemberPath"/> property.
/// </summary>
public static readonly DependencyProperty TextMemberPathProperty = DependencyProperty.Register(
nameof(TextMemberPath),
typeof(string),
typeof(TokenizingTextBox),
new PropertyMetadata(null));
/// <summary>
/// Identifies the <see cref="TokenItemTemplate"/> property.
/// </summary>
public static readonly DependencyProperty TokenItemTemplateProperty = DependencyProperty.Register(
nameof(TokenItemTemplate),
typeof(DataTemplate),
typeof(TokenizingTextBox),
new PropertyMetadata(null));
/// <summary>
/// Identifies the <see cref="TokenItemTemplateSelector"/> property.
/// </summary>
public static readonly DependencyProperty TokenItemTemplateSelectorProperty = DependencyProperty.Register(
nameof(TokenItemTemplateSelector),
typeof(DataTemplateSelector),
typeof(TokenizingTextBox),
new PropertyMetadata(null));
/// <summary>
/// Identifies the <see cref="TokenDelimiter"/> property.
/// </summary>
public static readonly DependencyProperty TokenDelimiterProperty = DependencyProperty.Register(
nameof(TokenDelimiter),
typeof(string),
typeof(TokenizingTextBox),
new PropertyMetadata(" "));
/// <summary>
/// Identifies the <see cref="TokenSpacing"/> property.
/// </summary>
public static readonly DependencyProperty TokenSpacingProperty = DependencyProperty.Register(
nameof(TokenSpacing),
typeof(double),
typeof(TokenizingTextBox),
new PropertyMetadata(null));
/// <summary>
/// Identifies the <see cref="PlaceholderText"/> property.
/// </summary>
public static readonly DependencyProperty PlaceholderTextProperty = DependencyProperty.Register(
nameof(PlaceholderText),
typeof(string),
typeof(TokenizingTextBox),
new PropertyMetadata(string.Empty));
/// <summary>
/// Identifies the <see cref="QueryIcon"/> property.
/// </summary>
public static readonly DependencyProperty QueryIconProperty = DependencyProperty.Register(
nameof(QueryIcon),
typeof(IconSource),
typeof(TokenizingTextBox),
new PropertyMetadata(null));
/// <summary>
/// Identifies the <see cref="Text"/> property.
/// </summary>
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
nameof(Text),
typeof(string),
typeof(TokenizingTextBox),
new PropertyMetadata(string.Empty, TextPropertyChanged));
private static void TextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TokenizingTextBox ttb && ttb._currentTextEdit != null)
{
ttb._currentTextEdit.Text = e.NewValue as string;
// Notify inner container of text change, see issue #4749
var item = ttb.ContainerFromItem(ttb._currentTextEdit) as TokenizingTextBoxItem;
item?.UpdateText(ttb._currentTextEdit.Text);
}
}
/// <summary>
/// Identifies the <see cref="KeepTextAfterQuerySubmitted"/> property.
/// </summary>
public static readonly DependencyProperty KeepTextAfterQuerySubmittedProperty = DependencyProperty.Register(
nameof(KeepTextAfterQuerySubmitted),
typeof(bool),
typeof(TokenizingTextBox),
new PropertyMetadata(false));
/// <summary>
/// Identifies the <see cref="SuggestedItemsSource"/> property.
/// </summary>
public static readonly DependencyProperty SuggestedItemsSourceProperty = DependencyProperty.Register(
nameof(SuggestedItemsSource),
typeof(object),
typeof(TokenizingTextBox),
new PropertyMetadata(null));
/// <summary>
/// Identifies the <see cref="SuggestedItemTemplate"/> property.
/// </summary>
public static readonly DependencyProperty SuggestedItemTemplateProperty = DependencyProperty.Register(
nameof(SuggestedItemTemplate),
typeof(DataTemplate),
typeof(TokenizingTextBox),
new PropertyMetadata(null));
/// <summary>
/// Identifies the <see cref="SuggestedItemTemplateSelector"/> property.
/// </summary>
public static readonly DependencyProperty SuggestedItemTemplateSelectorProperty = DependencyProperty.Register(
nameof(SuggestedItemTemplateSelector),
typeof(DataTemplateSelector),
typeof(TokenizingTextBox),
new PropertyMetadata(null));
/// <summary>
/// Identifies the <see cref="SuggestedItemContainerStyle"/> property.
/// </summary>
public static readonly DependencyProperty SuggestedItemContainerStyleProperty = DependencyProperty.Register(
nameof(SuggestedItemContainerStyle),
typeof(Style),
typeof(TokenizingTextBox),
new PropertyMetadata(null));
/// <summary>
/// Identifies the <see cref="TabNavigateBackOnArrow"/> property.
/// </summary>
public static readonly DependencyProperty TabNavigateBackOnArrowProperty = DependencyProperty.Register(
nameof(TabNavigateBackOnArrow),
typeof(bool),
typeof(TokenizingTextBox),
new PropertyMetadata(false));
/// <summary>
/// Identifies the <see cref="MaximumTokens"/> property.
/// </summary>
public static readonly DependencyProperty MaximumTokensProperty = DependencyProperty.Register(
nameof(MaximumTokens),
typeof(int),
typeof(TokenizingTextBox),
new PropertyMetadata(null, OnMaximumTokensChanged));
private static void OnMaximumTokensChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TokenizingTextBox ttb && ttb.ReadLocalValue(MaximumTokensProperty) != DependencyProperty.UnsetValue && e.NewValue is int newMaxTokens)
{
var tokenCount = ttb._innerItemsSource.ItemsSource.Count;
if (tokenCount > 0 && tokenCount > newMaxTokens)
{
int tokensToRemove = tokenCount - Math.Max(newMaxTokens, 0);
// Start at the end, remove any extra tokens.
for (var i = tokenCount; i > tokenCount - tokensToRemove; --i)
{
var token = ttb._innerItemsSource.ItemsSource[i - 1];
// Force remove the items. No warning and no option to cancel.
ttb._innerItemsSource.Remove(token);
ttb.TokenItemRemoved?.Invoke(ttb, token);
}
}
}
}
/// <summary>
/// Gets or sets the Style for the contained AutoSuggestBox template part.
/// </summary>
public Style AutoSuggestBoxStyle
{
get => (Style)GetValue(AutoSuggestBoxStyleProperty);
set => SetValue(AutoSuggestBoxStyleProperty, value);
}
/// <summary>
/// Gets or sets the Style for the TextBox part of the AutoSuggestBox template part.
/// </summary>
public Style AutoSuggestBoxTextBoxStyle
{
get => (Style)GetValue(AutoSuggestBoxStyleProperty);
set => SetValue(AutoSuggestBoxStyleProperty, value);
}
/// <summary>
/// Gets or sets the TextMemberPath of the AutoSuggestBox template part.
/// </summary>
public string TextMemberPath
{
get => (string)GetValue(TextMemberPathProperty);
set => SetValue(TextMemberPathProperty, value);
}
/// <summary>
/// Gets or sets the template for token items.
/// </summary>
public DataTemplate TokenItemTemplate
{
get => (DataTemplate)GetValue(TokenItemTemplateProperty);
set => SetValue(TokenItemTemplateProperty, value);
}
/// <summary>
/// Gets or sets the template selector for token items.
/// </summary>
public DataTemplateSelector TokenItemTemplateSelector
{
get => (DataTemplateSelector)GetValue(TokenItemTemplateSelectorProperty);
set => SetValue(TokenItemTemplateSelectorProperty, value);
}
/// <summary>
/// Gets or sets delimiter used to determine when to process text input as a new token item.
/// </summary>
public string TokenDelimiter
{
get => (string)GetValue(TokenDelimiterProperty);
set => SetValue(TokenDelimiterProperty, value);
}
/// <summary>
/// Gets or sets the spacing value used to separate token items.
/// </summary>
public double TokenSpacing
{
get => (double)GetValue(TokenSpacingProperty);
set => SetValue(TokenSpacingProperty, value);
}
/// <summary>
/// Gets or sets the PlaceholderText for the AutoSuggestBox template part.
/// </summary>
public string PlaceholderText
{
get => (string)GetValue(PlaceholderTextProperty);
set => SetValue(PlaceholderTextProperty, value);
}
/// <summary>
/// Gets or sets the icon to display in the AutoSuggestBox template part.
/// </summary>
public IconSource QueryIcon
{
get => (IconSource)GetValue(QueryIconProperty);
set => SetValue(QueryIconProperty, value);
}
/// <summary>
/// Gets or sets the input text of the currently active text edit.
/// </summary>
public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
/// <summary>
/// Gets or sets a value indicating whether the control should keep or clear the text
/// after the query is submitted. Default is false (to clear the text).
/// </summary>
public bool KeepTextAfterQuerySubmitted
{
get => (bool)GetValue(KeepTextAfterQuerySubmittedProperty);
set => SetValue(KeepTextAfterQuerySubmittedProperty, value);
}
/// <summary>
/// Gets or sets the items source for token suggestions.
/// </summary>
public object SuggestedItemsSource
{
get => GetValue(SuggestedItemsSourceProperty);
set => SetValue(SuggestedItemsSourceProperty, value);
}
/// <summary>
/// Gets or sets the template for displaying suggested tokens.
/// </summary>
public DataTemplate SuggestedItemTemplate
{
get => (DataTemplate)GetValue(SuggestedItemTemplateProperty);
set => SetValue(SuggestedItemTemplateProperty, value);
}
/// <summary>
/// Gets or sets the template selector for displaying suggested tokens.
/// </summary>
public DataTemplateSelector SuggestedItemTemplateSelector
{
get => (DataTemplateSelector)GetValue(SuggestedItemTemplateSelectorProperty);
set => SetValue(SuggestedItemTemplateSelectorProperty, value);
}
/// <summary>
/// Gets or sets the item container style for displaying suggested tokens.
/// </summary>
public Style SuggestedItemContainerStyle
{
get => (Style)GetValue(SuggestedItemContainerStyleProperty);
set => SetValue(SuggestedItemContainerStyleProperty, value);
}
/// <summary>
/// Gets or sets a value indicating whether the control will move focus to the previous
/// control when an arrow key is pressed and selection is at one of the limits in the control.
/// </summary>
public bool TabNavigateBackOnArrow
{
get => (bool)GetValue(TabNavigateBackOnArrowProperty);
set => SetValue(TabNavigateBackOnArrowProperty, value);
}
/// <summary>
/// Gets the complete text value of any selection in the control. The result is the same text as would be copied to the clipboard.
/// </summary>
public string SelectedTokenText
{
get
{
return PrepareSelectionForClipboard();
}
}
/// <summary>
/// Gets or sets the maximum number of token results allowed at a time.
/// </summary>
public int MaximumTokens
{
get => (int)GetValue(MaximumTokensProperty);
set => SetValue(MaximumTokensProperty, value);
}
}
}