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 ;
56using Windows . Foundation ;
67using Windows . System ;
78using Windows . UI ;
@@ -100,29 +101,36 @@ private void OnApplyTemplateAutoSuggestBox(AutoSuggestBox auto)
100101 _autoSuggestBox . LostFocus += AutoSuggestBox_LostFocus ;
101102
102103 // Setup a binding to the QueryIcon of the Parent if we're the last box.
103- if ( Content is ITokenStringContainer str && str . IsLast )
104+ if ( Content is ITokenStringContainer str )
104105 {
105- // Workaround for https://github.com/microsoft/microsoft-ui-xaml/issues/2568
106- if ( Owner . QueryIcon is FontIconSource fis &&
107- fis . ReadLocalValue ( FontIconSource . FontSizeProperty ) == DependencyProperty . UnsetValue )
108- {
109- // This can be expensive, could we optimize?
110- // Also, this is changing the FontSize on the IconSource (which could be shared?)
111- fis . FontSize = Owner . TryFindResource ( "TokenizingTextBoxIconFontSize" ) as double ? ?? 16 ;
112- }
106+ // We need to set our initial text in all cases.
107+ _autoSuggestBox . Text = str . Text ;
113108
114- var iconBinding = new Binding ( )
109+ // We only set/bind some properties on the last textbox to mimic the autosuggestbox look
110+ if ( str . IsLast )
115111 {
116- Source = Owner ,
117- Path = new PropertyPath ( nameof ( Owner . QueryIcon ) ) ,
118- RelativeSource = new RelativeSource ( ) { Mode = RelativeSourceMode . TemplatedParent }
119- } ;
112+ // Workaround for https://github.com/microsoft/microsoft-ui-xaml/issues/2568
113+ if ( Owner . QueryIcon is FontIconSource fis &&
114+ fis . ReadLocalValue ( FontIconSource . FontSizeProperty ) == DependencyProperty . UnsetValue )
115+ {
116+ // This can be expensive, could we optimize?
117+ // Also, this is changing the FontSize on the IconSource (which could be shared?)
118+ fis . FontSize = Owner . TryFindResource ( "TokenizingTextBoxIconFontSize" ) as double ? ?? 16 ;
119+ }
120120
121- var iconSourceElement = new IconSourceElement ( ) ;
121+ var iconBinding = new Binding ( )
122+ {
123+ Source = Owner ,
124+ Path = new PropertyPath ( nameof ( Owner . QueryIcon ) ) ,
125+ RelativeSource = new RelativeSource ( ) { Mode = RelativeSourceMode . TemplatedParent }
126+ } ;
127+
128+ var iconSourceElement = new IconSourceElement ( ) ;
122129
123- iconSourceElement . SetBinding ( IconSourceElement . IconSourceProperty , iconBinding ) ;
130+ iconSourceElement . SetBinding ( IconSourceElement . IconSourceProperty , iconBinding ) ;
124131
125- _autoSuggestBox . QueryIcon = iconSourceElement ;
132+ _autoSuggestBox . QueryIcon = iconSourceElement ;
133+ }
126134 }
127135 }
128136 }
@@ -156,11 +164,35 @@ private void AutoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestB
156164 Owner . RaiseSuggestionChosen ( sender , args ) ;
157165 }
158166
159- private void AutoSuggestBox_TextChanged ( AutoSuggestBox sender , AutoSuggestBoxTextChangedEventArgs args )
167+ // Called to update text by link:TokenizingTextBox.Properties.cs:TextPropertyChanged
168+ internal void UpdateText ( string text )
160169 {
161- var t = sender . Text . Trim ( ) ;
170+ if ( _autoSuggestBox != null )
171+ {
172+ _autoSuggestBox . Text = text ;
173+ }
174+ else
175+ {
176+ void WaitForLoad ( object s , RoutedEventArgs eargs )
177+ {
178+ if ( _autoSuggestTextBox != null )
179+ {
180+ _autoSuggestTextBox . Text = text ;
181+ }
162182
163- Owner . Text = sender . Text ; // Update parent text property
183+ AutoSuggestTextBoxLoaded -= WaitForLoad ;
184+ }
185+
186+ AutoSuggestTextBoxLoaded += WaitForLoad ;
187+ }
188+ }
189+
190+ private void AutoSuggestBox_TextChanged ( AutoSuggestBox sender , AutoSuggestBoxTextChangedEventArgs args )
191+ {
192+ if ( ! EqualityComparer < string > . Default . Equals ( sender . Text , Owner . Text ) )
193+ {
194+ Owner . Text = sender . Text ; // Update parent text property, if different
195+ }
164196
165197 // Override our programmatic manipulation as we're redirecting input for the user
166198 if ( UseCharacterAsUser )
@@ -172,6 +204,8 @@ private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTex
172204
173205 Owner . RaiseTextChanged ( sender , args ) ;
174206
207+ var t = sender . Text ? . Trim ( ) ?? string . Empty ;
208+
175209 // Look for Token Delimiters to create new tokens when text changes.
176210 if ( ! string . IsNullOrEmpty ( Owner . TokenDelimiter ) && t . Contains ( Owner . TokenDelimiter ) )
177211 {
@@ -195,7 +229,7 @@ private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTex
195229 }
196230 else
197231 {
198- sender . Text = tokens [ tokens . Length - 1 ] ;
232+ sender . Text = tokens [ tokens . Length - 1 ] . Trim ( ) ;
199233 }
200234 }
201235 }
0 commit comments