Populating the Text
property of the AutoCompleteBox
via a data binding can be problematic, since when a suggestion is chosen the binding is effectively removed and therefore needs to be restored.
You can reapply a binding by subscribing to the SelectionChanged
event of the AutoCompleteBox
.
void AutoCompleteBox_SelectionChanged(
object sender, SelectionChangedEventArgs e)
{
AutoCompleteBox autoCompleteBox = (AutoCompleteBox)sender;
Dispatcher.BeginInvoke(delegate
{
Binding binding = new Binding("YourViewModelTextProperty");
binding.Source = autoCompleteBox.DataContext;
autoCompleteBox.SetBinding(AutoCompleteBox.TextProperty, binding);
});
}
When the AutoCompleteBox.IsTextCompletionEnabled
property is set to true, the AutoCompleteBox.KeyUp
event can be used instead of the SelectionChanged
event to ensure that the binding is restored regardless of whether a selection is made.
18.116.19.17