3

I use AutoCompleteBox in MVVM and i want to execute something only if the user click on the Item or if the user press Enter.

But now when I use the down\Up Key on the keyboard the selectedItem property changes...

My controls :

<Controls:AutoCompleteBox ItemsSource="{Binding IndicationDtos, Mode=TwoWay}" 
                              Width="100" SelectedItem="{Binding IndicationSelected, Mode=TwoWay}" 
                              ValueMemberPath="Diagnosis" Text="{Binding Criteria, Mode=TwoWay}" MinimumPopulateDelay="250"/>

What can I do to make the property "SelectedItem" is assigned only on Enter or click?

If you have any question...

thanks a lot

Benjamin
  • 129
  • 8

2 Answers2

0

In your SelectedItem binding, you can use:

SelectedItem="{Binding IndicationSelected, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"  

That way selected item only changes when you focus on something else

Chris Woolum
  • 2,854
  • 20
  • 20
0

I found solution i created new class.

Like this :

    public class AutoCompleteBoxEx : AutoCompleteBox
{
            public static readonly DependencyProperty SelectionBoxItemProperty =
        DependencyProperty.Register(
        "SelectionBoxItem",
        typeof(object),
        typeof(AutoCompleteBox),
        new PropertyMetadata(OnSelectionBoxItemPropertyChanged));

    public object SelectionBoxItem
    {
        get
        {
            return GetValue(SelectionBoxItemProperty);
        }

        set
        {
            SetValue(SelectionBoxItemProperty, value);
        }
    }

    protected override void OnDropDownClosing(RoutedPropertyChangingEventArgs<bool> e)
    {
        base.OnDropDownClosing(e);
        SelectionBoxItem = SelectionAdapter.SelectedItem;
    }

    private static void OnSelectionBoxItemPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
    }
}
Benjamin
  • 129
  • 8