Data Binding and the AutoCompleteBox.Text Property

,

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.19.17