When I use AutoCompleteBox in MVVM scenario I bind SelectedItem to a ViewModel. In the following scenario SearchText is shown in TextBox part instead of expected empty string:
1. User enters something in AutoCompleteBox (say "john") and then selects an item from dropdown (say "John Smith").
2. Later my code sets null to a ViewModel property to which AutoCompleteBox.SelectedItem is bound.
3. AutoCompleteBox shows old SearchText ("john") instead of expected empty string.
I'm aware of workaround of setting Text=string.Empty before setting SelectedItem=null. This workaournd can even be implemented in MVVM by having Text bound to a special VM property, but this is very ugly in my opinion.
I also tried to subclass the AutoCompleteBox to override the bogus behavior, but all required methods and properties are either private or non-virtual, so I was not able to fix this by subclassing too.
Comments: ** Comment from web user: arabelu **
1. User enters something in AutoCompleteBox (say "john") and then selects an item from dropdown (say "John Smith").
2. Later my code sets null to a ViewModel property to which AutoCompleteBox.SelectedItem is bound.
3. AutoCompleteBox shows old SearchText ("john") instead of expected empty string.
I'm aware of workaround of setting Text=string.Empty before setting SelectedItem=null. This workaournd can even be implemented in MVVM by having Text bound to a special VM property, but this is very ugly in my opinion.
I also tried to subclass the AutoCompleteBox to override the bogus behavior, but all required methods and properties are either private or non-virtual, so I was not able to fix this by subclassing too.
Comments: ** Comment from web user: arabelu **
To reset the SearchText for the AutoCompleteBox you should set the SelectedItem to the default value of the type and not to "null".
If you bind the -> AutoCompleteBox to a list of string then the SelectedItem = string.Empty
-> AutoCompleteBox to a list of typed objects then the SelectedItem = default constructuor of the typed object