@@ -34,6 +34,7 @@ public class AutoCompleteTextBox : Control
3434 public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty . Register ( "ItemTemplate" , typeof ( DataTemplate ) , typeof ( AutoCompleteTextBox ) , new FrameworkPropertyMetadata ( null ) ) ;
3535 public static readonly DependencyProperty ItemTemplateSelectorProperty = DependencyProperty . Register ( "ItemTemplateSelector" , typeof ( DataTemplateSelector ) , typeof ( AutoCompleteTextBox ) ) ;
3636 public static readonly DependencyProperty LoadingContentProperty = DependencyProperty . Register ( "LoadingContent" , typeof ( object ) , typeof ( AutoCompleteTextBox ) , new FrameworkPropertyMetadata ( null ) ) ;
37+ public static readonly DependencyProperty PreviewSelectionProperty = DependencyProperty . Register ( "PreviewSelection" , typeof ( bool ) , typeof ( AutoCompleteTextBox ) , new FrameworkPropertyMetadata ( true ) ) ;
3738 public static readonly DependencyProperty ProviderProperty = DependencyProperty . Register ( "Provider" , typeof ( ISuggestionProvider ) , typeof ( AutoCompleteTextBox ) , new FrameworkPropertyMetadata ( null ) ) ;
3839 public static readonly DependencyProperty SelectedItemProperty = DependencyProperty . Register ( "SelectedItem" , typeof ( object ) , typeof ( AutoCompleteTextBox ) , new FrameworkPropertyMetadata ( null , OnSelectedItemChanged ) ) ;
3940 public static readonly DependencyProperty TextProperty = DependencyProperty . Register ( "Text" , typeof ( string ) , typeof ( AutoCompleteTextBox ) , new FrameworkPropertyMetadata ( string . Empty , propertyChangedCallback : null , coerceValueCallback : null , isAnimationProhibited : false , defaultUpdateSourceTrigger : UpdateSourceTrigger . LostFocus , flags : FrameworkPropertyMetadataOptions . BindsTwoWayByDefault ) ) ;
@@ -182,6 +183,12 @@ public object LoadingContent
182183 }
183184
184185 public Popup Popup { get ; set ; }
186+
187+ public bool PreviewSelection
188+ {
189+ get => ( bool ) GetValue ( PreviewSelectionProperty ) ;
190+ set => SetValue ( PreviewSelectionProperty , value ) ;
191+ }
185192
186193 public ISuggestionProvider Provider
187194 {
@@ -436,28 +443,31 @@ private void OnPopupOpened(object sender, EventArgs e)
436443 }
437444
438445 private void OnSelectionAdapterCancel ( SelectionAdapter . EventCause cause )
439- {
446+ {
440447 if ( PreSelectionEventSomeoneHandled ( cause , true ) )
441448 return ;
442449
450+ IsDropDownOpen = false ;
451+ _selectionCancelled = true ;
452+
453+ if ( ! PreviewSelection )
454+ return ;
455+
443456 _isUpdatingText = true ;
444457 Editor . Text = SelectedItem == null ? Filter : GetDisplayText ( SelectedItem ) ;
445458 Editor . SelectionStart = Editor . Text . Length ;
446459 Editor . SelectionLength = 0 ;
447460 _isUpdatingText = false ;
448- IsDropDownOpen = false ;
449- _selectionCancelled = true ;
461+ }
450462
451- }
452-
453463 public event EventHandler < SelectionAdapter . PreSelectionAdapterFinishArgs > PreSelectionAdapterFinish ;
454- private bool PreSelectionEventSomeoneHandled ( SelectionAdapter . EventCause cause , bool is_cancel ) {
455- if ( PreSelectionAdapterFinish == null )
456- return false ;
457- var args = new SelectionAdapter . PreSelectionAdapterFinishArgs { cause = cause , is_cancel = is_cancel } ;
458- PreSelectionAdapterFinish ? . Invoke ( this , args ) ;
459- return args . handled ;
460-
464+ private bool PreSelectionEventSomeoneHandled ( SelectionAdapter . EventCause cause , bool is_cancel ) {
465+ if ( PreSelectionAdapterFinish == null )
466+ return false ;
467+ var args = new SelectionAdapter . PreSelectionAdapterFinishArgs { cause = cause , is_cancel = is_cancel } ;
468+ PreSelectionAdapterFinish ? . Invoke ( this , args ) ;
469+ return args . handled ;
470+
461471 }
462472 private void OnSelectionAdapterCommit ( SelectionAdapter . EventCause cause )
463473 {
@@ -469,6 +479,8 @@ private void OnSelectionAdapterCommit(SelectionAdapter.EventCause cause)
469479 SelectedItem = ItemsSelector . SelectedItem ;
470480 _isUpdatingText = true ;
471481 Editor . Text = GetDisplayText ( ItemsSelector . SelectedItem ) ;
482+ Editor . SelectionStart = Editor . Text . Length ;
483+ Editor . SelectionLength = 0 ;
472484 SetSelectedItem ( ItemsSelector . SelectedItem ) ;
473485 _isUpdatingText = false ;
474486 IsDropDownOpen = false ;
@@ -477,6 +489,9 @@ private void OnSelectionAdapterCommit(SelectionAdapter.EventCause cause)
477489
478490 private void OnSelectionAdapterSelectionChanged ( )
479491 {
492+ if ( ! PreviewSelection )
493+ return ;
494+
480495 _isUpdatingText = true ;
481496 Editor . Text = ItemsSelector . SelectedItem == null ? Filter : GetDisplayText ( ItemsSelector . SelectedItem ) ;
482497 Editor . SelectionStart = Editor . Text . Length ;
0 commit comments