Skip to content

Commit fafbf5b

Browse files
committed
Added support for TokenSelectionMode in TokenizingTextBox
1 parent 794b295 commit fafbf5b

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/TokenizingTextBox/TokenizingTextBoxXaml.bind

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
MaxHeight="104"
4040
HorizontalAlignment="Stretch"
4141
TextMemberPath="Text"
42-
TokenDelimiter=",">
42+
TokenDelimiter=","
43+
TokenSelectionMode="Single">
4344
<controls:TokenizingTextBox.SuggestedItemTemplate>
4445
<DataTemplate>
4546
<StackPanel Orientation="Horizontal">
@@ -75,6 +76,7 @@
7576
QueryIcon="{ui:SymbolIconSource Symbol=Find}"
7677
TextMemberPath="Text"
7778
TokenDelimiter=","
79+
TokenSelectionMode="Multiple"
7880
IsItemClickEnabled="True"
7981
TokenItemTemplate="{StaticResource EmailTokenTemplate}">
8082
</controls:TokenizingTextBox>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace Microsoft.Toolkit.Uwp.UI.Controls
12+
{
13+
/// <summary>
14+
/// Indicates how tokens are selected in the <see cref="TokenizingTextBox"/>.
15+
/// </summary>
16+
public enum TokenSelectionMode
17+
{
18+
/// <summary>
19+
/// Only one token can be selected at a time. A new token should replace the active selection.
20+
/// </summary>
21+
Single,
22+
23+
/// <summary>
24+
/// Multiple tokens can be selected at a time.
25+
/// </summary>
26+
Multiple,
27+
}
28+
}

Microsoft.Toolkit.Uwp.UI.Controls.Input/TokenizingTextBox/TokenizingTextBox.Properties.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,26 @@ private static void TextPropertyChanged(DependencyObject d, DependencyPropertyCh
157157
typeof(TokenizingTextBox),
158158
new PropertyMetadata(false));
159159

160+
/// <summary>
161+
/// Identifies the <see cref="TokenSelectionMode"/> property.
162+
/// </summary>
163+
public static readonly DependencyProperty TokenSelectionModeProperty = DependencyProperty.Register(
164+
nameof(TokenSelectionMode),
165+
typeof(TokenSelectionMode),
166+
typeof(TokenizingTextBox),
167+
new PropertyMetadata(TokenSelectionMode.Multiple, OnTokenSelectionModeChanged));
168+
169+
private static void OnTokenSelectionModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
170+
{
171+
if (d is TokenizingTextBox ttb && e.NewValue is TokenSelectionMode newTokenSelectionMode && newTokenSelectionMode == TokenSelectionMode.Single)
172+
{
173+
while (ttb.Items.Count > 1)
174+
{
175+
ttb.Items.RemoveAt(ttb.Items.Count - 1);
176+
}
177+
}
178+
}
179+
160180
/// <summary>
161181
/// Gets or sets the Style for the contained AutoSuggestBox template part.
162182
/// </summary>
@@ -303,5 +323,16 @@ public string SelectedTokenText
303323
return PrepareSelectionForClipboard();
304324
}
305325
}
326+
327+
/// <summary>
328+
/// Gets or sets how the control should display tokens.
329+
/// <see cref="TokenSelectionMode.Multiple"/> is the default. Multiple tokens can be selected at a time.
330+
/// <see cref="TokenSelectionMode.Single"/> indicates that only one token can be present in the control at a time.
331+
/// </summary>
332+
public TokenSelectionMode TokenSelectionMode
333+
{
334+
get => (TokenSelectionMode)GetValue(TokenSelectionModeProperty);
335+
set => SetValue(TokenSelectionModeProperty, value);
336+
}
306337
}
307338
}

Microsoft.Toolkit.Uwp.UI.Controls.Input/TokenizingTextBox/TokenizingTextBox.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,19 @@ internal async Task AddTokenAsync(object data, bool? atEnd = null)
448448
}
449449
}
450450

451+
if (TokenSelectionMode == TokenSelectionMode.Single)
452+
{
453+
// Remove any existing tokens.
454+
for (var i = _innerItemsSource.Count - 1; i >= 0; --i)
455+
{
456+
var item = _innerItemsSource[i];
457+
if (item is not ITokenStringContainer)
458+
{
459+
_innerItemsSource.Remove(item);
460+
}
461+
}
462+
}
463+
451464
// If we've been typing in the last box, just add this to the end of our collection
452465
if (atEnd == true || _currentTextEdit == _lastTextEdit)
453466
{

0 commit comments

Comments
 (0)