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: atsukanrock **
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: atsukanrock **
The workaround shown in the original description does not work under a certain condition.
- The Workaround
setting Text=string.Empty before setting SelectedItem=null.
- The Condition under Which The Workaround Does Not Work
The MinimumPrefixLength of the AutoCompleteBox is 0.
- Solution
setting MinimumPrefixLength other than 0 before setting Text = string.Empty.
We can reset the MinimumPrefixLength after setting Text = string.Empty if needed.