@@ -132,12 +132,15 @@ public event RoutedPropertyChangedEventHandler<object> SuggestionChosen
132
132
133
133
public override void OnApplyTemplate ( )
134
134
{
135
+ if ( _autoSuggestBoxList is not null )
136
+ {
137
+ _autoSuggestBoxList . PreviewMouseDown -= AutoSuggestionListBox_PreviewMouseDown ;
138
+ }
139
+
135
140
if ( GetTemplateChild ( AutoSuggestBoxListPart ) is ListBox listBox )
136
141
{
137
142
_autoSuggestBoxList = listBox ;
138
143
139
- listBox . PreviewMouseDown -= AutoSuggestionListBox_PreviewMouseDown ;
140
-
141
144
base . OnApplyTemplate ( ) ;
142
145
143
146
listBox . PreviewMouseDown += AutoSuggestionListBox_PreviewMouseDown ;
@@ -195,39 +198,57 @@ private void AutoSuggestionListBox_PreviewMouseDown(object sender, MouseButtonEv
195
198
{
196
199
if ( _autoSuggestBoxList is not null && e . OriginalSource is FrameworkElement element )
197
200
{
198
- if ( ! _autoSuggestBoxList . Items . Contains ( element . DataContext ) )
201
+ var selectedItem = element . DataContext ;
202
+ if ( ! _autoSuggestBoxList . Items . Contains ( selectedItem ) )
199
203
return ;
200
- _autoSuggestBoxList . SelectedItem = element . DataContext ;
201
- CommitValueSelection ( ) ;
204
+ if ( _autoSuggestBoxList . SelectedItem != selectedItem )
205
+ {
206
+ _autoSuggestBoxList . SelectionChanged += OnSelectionChanged ;
207
+ _autoSuggestBoxList . SelectedItem = selectedItem ;
208
+ }
209
+ else
210
+ {
211
+ _autoSuggestBoxList . SelectedItem = selectedItem ;
212
+ CommitValueSelection ( ) ;
213
+ }
214
+
215
+ void OnSelectionChanged ( object sender , SelectionChangedEventArgs e )
216
+ {
217
+ _autoSuggestBoxList . SelectionChanged -= OnSelectionChanged ;
218
+ CommitValueSelection ( ) ;
219
+ }
220
+ e . Handled = true ;
202
221
}
203
222
}
204
223
224
+
225
+
205
226
#endregion
206
227
207
- #region Methods
228
+ #region Methods
208
229
209
230
private void CloseAutoSuggestionPopUp ( )
210
231
{
211
232
IsSuggestionOpen = false ;
212
233
}
213
234
214
235
private void CommitValueSelection ( )
236
+ => CommitValueSelection ( _autoSuggestBoxList ? . SelectedValue ) ;
237
+
238
+ private void CommitValueSelection ( object ? selectedValue )
215
239
{
216
- if ( _autoSuggestBoxList ? . SelectedValue is { } selectedValue )
240
+ string oldValue = Text ;
241
+ Text = selectedValue ? . ToString ( ) ;
242
+ if ( Text != null )
217
243
{
218
- var oldValue = Text ;
219
- Text = selectedValue . ToString ( ) ;
220
- if ( Text != null )
221
- {
222
- CaretIndex = Text . Length ;
223
- }
224
- CloseAutoSuggestionPopUp ( ) ;
225
- var args = new RoutedPropertyChangedEventArgs < object ? > ( oldValue , Text )
226
- {
227
- RoutedEvent = SuggestionChosenEvent
228
- } ;
229
- RaiseEvent ( args ) ;
244
+ CaretIndex = Text . Length ;
230
245
}
246
+ CloseAutoSuggestionPopUp ( ) ;
247
+ var args = new RoutedPropertyChangedEventArgs < object ? > ( oldValue , Text )
248
+ {
249
+ RoutedEvent = SuggestionChosenEvent
250
+ } ;
251
+ RaiseEvent ( args ) ;
231
252
}
232
253
233
254
private void DecrementSelection ( )
0 commit comments