Skip to content

Commit fbeb2c4

Browse files
committed
Added TokenItemRemoved events
1 parent fafbf5b commit fbeb2c4

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,16 @@ private static void OnTokenSelectionModeChanged(DependencyObject d, DependencyPr
170170
{
171171
if (d is TokenizingTextBox ttb && e.NewValue is TokenSelectionMode newTokenSelectionMode && newTokenSelectionMode == TokenSelectionMode.Single)
172172
{
173-
while (ttb.Items.Count > 1)
173+
// Start at the end, remove all but the first token.
174+
for (var i = ttb._innerItemsSource.Count - 1; i >= 1; --i)
174175
{
175-
ttb.Items.RemoveAt(ttb.Items.Count - 1);
176+
var item = ttb._innerItemsSource[i];
177+
if (item is not ITokenStringContainer)
178+
{
179+
// Force remove the items. No warning and no option to cancel.
180+
ttb._innerItemsSource.Remove(item);
181+
ttb.TokenItemRemoved?.Invoke(ttb, item);
182+
}
176183
}
177184
}
178185
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,13 +450,15 @@ internal async Task AddTokenAsync(object data, bool? atEnd = null)
450450

451451
if (TokenSelectionMode == TokenSelectionMode.Single)
452452
{
453-
// Remove any existing tokens.
453+
// Start at the end, remove any existing tokens.
454454
for (var i = _innerItemsSource.Count - 1; i >= 0; --i)
455455
{
456456
var item = _innerItemsSource[i];
457457
if (item is not ITokenStringContainer)
458458
{
459+
// Force remove the items. No warning and no option to cancel.
459460
_innerItemsSource.Remove(item);
461+
TokenItemRemoved?.Invoke(this, item);
460462
}
461463
}
462464
}

0 commit comments

Comments
 (0)