Skip to content

Commit f0d8b2a

Browse files
committed
Made MaxTokens non-nullable and added checks for DP.UnsetValue
1 parent 358ada4 commit f0d8b2a

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,13 @@ private static void TextPropertyChanged(DependencyObject d, DependencyPropertyCh
162162
/// </summary>
163163
public static readonly DependencyProperty MaxTokensProperty = DependencyProperty.Register(
164164
nameof(MaxTokens),
165-
typeof(int?),
165+
typeof(int),
166166
typeof(TokenizingTextBox),
167-
new PropertyMetadata(null, OnMaxTokensChanged));
167+
new PropertyMetadata(DependencyProperty.UnsetValue, OnMaxTokensChanged));
168168

169169
private static void OnMaxTokensChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
170170
{
171-
if (d is TokenizingTextBox ttb && e.NewValue is int newMaxTokens)
171+
if (d is TokenizingTextBox ttb && ttb.ReadLocalValue(MaxTokensProperty) != DependencyProperty.UnsetValue && e.NewValue is int newMaxTokens)
172172
{
173173
var tokenCount = ttb.Items.Count;
174174
if (tokenCount > newMaxTokens)
@@ -347,9 +347,9 @@ public string SelectedTokenText
347347
/// <summary>
348348
/// Gets or sets the maximum number of token results allowed at a time.
349349
/// </summary>
350-
public int? MaxTokens
350+
public int MaxTokens
351351
{
352-
get => (int?)GetValue(MaxTokensProperty);
352+
get => (int)GetValue(MaxTokensProperty);
353353
set => SetValue(MaxTokensProperty, value);
354354
}
355355
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ private void ItemsSource_PropertyChanged(DependencyObject sender, DependencyProp
7878
{
7979
_innerItemsSource = new InterspersedObservableCollection(ItemsSource);
8080

81-
if (MaxTokens.HasValue && _innerItemsSource.ItemsSource.Count > MaxTokens)
81+
if (ReadLocalValue(MaxTokensProperty) != DependencyProperty.UnsetValue && _innerItemsSource.ItemsSource.Count > MaxTokens)
8282
{
8383
// Reduce down to the max as necessary.
84-
for (var i = _innerItemsSource.ItemsSource.Count - 1; i >= Math.Max(MaxTokens.Value, 0); --i)
84+
for (var i = _innerItemsSource.ItemsSource.Count - 1; i >= Math.Max(MaxTokens, 0); --i)
8585
{
8686
_innerItemsSource.Remove(_innerItemsSource[i]);
8787
}
@@ -440,7 +440,7 @@ public async Task ClearAsync()
440440

441441
internal async Task AddTokenAsync(object data, bool? atEnd = null)
442442
{
443-
if (MaxTokens <= 0)
443+
if (ReadLocalValue(MaxTokensProperty) != DependencyProperty.UnsetValue && MaxTokens <= 0)
444444
{
445445
// No tokens for you
446446
return;
@@ -465,7 +465,7 @@ internal async Task AddTokenAsync(object data, bool? atEnd = null)
465465
// If we've been typing in the last box, just add this to the end of our collection
466466
if (atEnd == true || _currentTextEdit == _lastTextEdit)
467467
{
468-
if (MaxTokens != null && _innerItemsSource.ItemsSource.Count >= MaxTokens)
468+
if (ReadLocalValue(MaxTokensProperty) != DependencyProperty.UnsetValue && _innerItemsSource.ItemsSource.Count >= MaxTokens)
469469
{
470470
// Remove tokens from the end until below the max number.
471471
for (var i = _innerItemsSource.Count - 2; i >= 0; --i)
@@ -493,7 +493,7 @@ internal async Task AddTokenAsync(object data, bool? atEnd = null)
493493
var edit = _currentTextEdit;
494494
var index = _innerItemsSource.IndexOf(edit);
495495

496-
if (MaxTokens != null && _innerItemsSource.ItemsSource.Count >= MaxTokens)
496+
if (ReadLocalValue(MaxTokensProperty) != DependencyProperty.UnsetValue && _innerItemsSource.ItemsSource.Count >= MaxTokens)
497497
{
498498
// Find the next token and remove it, until below the max number of tokens.
499499
for (var i = index; i < _innerItemsSource.Count; i++)

0 commit comments

Comments
 (0)