Skip to content

Commit c0ada84

Browse files
Fix issue #4749
Hook up initialization and change detection of parent Text property to update corresponding inner text of TokenizingTextBoxItem Failing tests from previous commit now pass.
1 parent 9f01895 commit c0ada84

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ private static void TextPropertyChanged(DependencyObject d, DependencyPropertyCh
109109
if (d is TokenizingTextBox ttb && ttb._currentTextEdit != null)
110110
{
111111
ttb._currentTextEdit.Text = e.NewValue as string;
112+
113+
// Notify inner container of text change, see issue #4749
114+
var item = ttb.ContainerFromItem(ttb._currentTextEdit) as TokenizingTextBoxItem;
115+
item?.UpdateText(ttb._currentTextEdit.Text);
112116
}
113117
}
114118

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ private void ItemsSource_PropertyChanged(DependencyObject sender, DependencyProp
8989
}
9090
}
9191

92-
_currentTextEdit = _lastTextEdit = new PretokenStringContainer(true);
92+
// Add our text box at the end of items and set it's default value to our initial text, fix for #4749
93+
_currentTextEdit = _lastTextEdit = new PretokenStringContainer(true) { Text = Text };
9394
_innerItemsSource.Insert(_innerItemsSource.Count, _currentTextEdit);
9495
ItemsSource = _innerItemsSource;
9596
}

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Collections.Generic;
56
using Windows.Foundation;
67
using Windows.System;
78
using Windows.UI;
@@ -123,6 +124,8 @@ private void OnApplyTemplateAutoSuggestBox(AutoSuggestBox auto)
123124
iconSourceElement.SetBinding(IconSourceElement.IconSourceProperty, iconBinding);
124125

125126
_autoSuggestBox.QueryIcon = iconSourceElement;
127+
128+
_autoSuggestBox.Text = str.Text;
126129
}
127130
}
128131
}
@@ -156,8 +159,40 @@ private void AutoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestB
156159
Owner.RaiseSuggestionChosen(sender, args);
157160
}
158161

162+
// Called to update text by link:TokenizingTextBox.Properties.cs:TextPropertyChanged
163+
internal void UpdateText(string text)
164+
{
165+
if (_autoSuggestBox != null)
166+
{
167+
_autoSuggestBox.Text = text;
168+
}
169+
else
170+
{
171+
void WaitForLoad(object s, RoutedEventArgs eargs)
172+
{
173+
if (_autoSuggestTextBox != null)
174+
{
175+
_autoSuggestTextBox.Text = text;
176+
}
177+
178+
AutoSuggestTextBoxLoaded -= WaitForLoad;
179+
}
180+
181+
AutoSuggestTextBoxLoaded += WaitForLoad;
182+
}
183+
}
184+
159185
private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
160186
{
187+
var hasDelimiter = !string.IsNullOrEmpty(Owner.TokenDelimiter) && sender.Text?.Contains(Owner.TokenDelimiter) == true;
188+
189+
// Ignore in the case we've been set from the parent and already equal the owning text,
190+
// unless we contain our delimiter.
191+
if (!hasDelimiter && EqualityComparer<string>.Default.Equals(sender.Text, Owner.Text))
192+
{
193+
return;
194+
}
195+
161196
var t = sender.Text.Trim();
162197

163198
Owner.Text = sender.Text; // Update parent text property
@@ -173,7 +208,7 @@ private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTex
173208
Owner.RaiseTextChanged(sender, args);
174209

175210
// Look for Token Delimiters to create new tokens when text changes.
176-
if (!string.IsNullOrEmpty(Owner.TokenDelimiter) && t.Contains(Owner.TokenDelimiter))
211+
if (hasDelimiter)
177212
{
178213
bool lastDelimited = t[t.Length - 1] == Owner.TokenDelimiter[0];
179214

0 commit comments

Comments
 (0)