Hook up initialization and change detection of parent Text property to update corresponding inner text of TokenizingTextBoxItem
Failing tests from previous commit now pass.
This commit is contained in:
michael-hawker 2022-10-17 16:18:42 -07:00 коммит произвёл Michael Hawker MSFT (XAML Llama)
Родитель 9f018956ea
Коммит c0ada84646
3 изменённых файлов: 42 добавлений и 2 удалений

Просмотреть файл

@ -109,6 +109,10 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
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);
}
}

Просмотреть файл

@ -89,7 +89,8 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
}
}
_currentTextEdit = _lastTextEdit = new PretokenStringContainer(true);
// Add our text box at the end of items and set it's default value to our initial text, fix for #4749
_currentTextEdit = _lastTextEdit = new PretokenStringContainer(true) { Text = Text };
_innerItemsSource.Insert(_innerItemsSource.Count, _currentTextEdit);
ItemsSource = _innerItemsSource;
}

Просмотреть файл

@ -2,6 +2,7 @@
// 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.Collections.Generic;
using Windows.Foundation;
using Windows.System;
using Windows.UI;
@ -123,6 +124,8 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
iconSourceElement.SetBinding(IconSourceElement.IconSourceProperty, iconBinding);
_autoSuggestBox.QueryIcon = iconSourceElement;
_autoSuggestBox.Text = str.Text;
}
}
}
@ -156,8 +159,40 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
Owner.RaiseSuggestionChosen(sender, args);
}
// Called to update text by link:TokenizingTextBox.Properties.cs:TextPropertyChanged
internal void UpdateText(string text)
{
if (_autoSuggestBox != null)
{
_autoSuggestBox.Text = text;
}
else
{
void WaitForLoad(object s, RoutedEventArgs eargs)
{
if (_autoSuggestTextBox != null)
{
_autoSuggestTextBox.Text = text;
}
AutoSuggestTextBoxLoaded -= WaitForLoad;
}
AutoSuggestTextBoxLoaded += WaitForLoad;
}
}
private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
var hasDelimiter = !string.IsNullOrEmpty(Owner.TokenDelimiter) && sender.Text?.Contains(Owner.TokenDelimiter) == true;
// Ignore in the case we've been set from the parent and already equal the owning text,
// unless we contain our delimiter.
if (!hasDelimiter && EqualityComparer<string>.Default.Equals(sender.Text, Owner.Text))
{
return;
}
var t = sender.Text.Trim();
Owner.Text = sender.Text; // Update parent text property
@ -173,7 +208,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
Owner.RaiseTextChanged(sender, args);
// Look for Token Delimiters to create new tokens when text changes.
if (!string.IsNullOrEmpty(Owner.TokenDelimiter) && t.Contains(Owner.TokenDelimiter))
if (hasDelimiter)
{
bool lastDelimited = t[t.Length - 1] == Owner.TokenDelimiter[0];