Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Hook up initialization and change detection of parent Text property to update corresponding inner text of TokenizingTextBoxItem
Failing tests from previous commit now pass.
  • Loading branch information
michael-hawker committed Oct 18, 2022
1 parent b38a000 commit 58caeb2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ private static void TextPropertyChanged(DependencyObject d, DependencyPropertyCh
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ private void ItemsSource_PropertyChanged(DependencyObject sender, DependencyProp
}
}

_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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -123,6 +124,8 @@ private void OnApplyTemplateAutoSuggestBox(AutoSuggestBox auto)
iconSourceElement.SetBinding(IconSourceElement.IconSourceProperty, iconBinding);

_autoSuggestBox.QueryIcon = iconSourceElement;

_autoSuggestBox.Text = str.Text;
}
}
}
Expand Down Expand Up @@ -156,8 +159,40 @@ private void AutoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestB
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
Expand All @@ -173,7 +208,7 @@ private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTex
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];

Expand Down

0 comments on commit 58caeb2

Please sign in to comment.