0
votes

This is my ComboBox:

<ComboBox x:Name="comboBoxExchange" HorizontalAlignment="Left" Margin="167,162,0,0" VerticalAlignment="Top" Width="222" SelectedItem="{Binding Exchange}"/>

This is the code I use to initialize the ComboBox:

private void InitExchange()
{
    comboBoxExchange.ItemsSource = (from p in m_DbSession.Query<Exchange>() orderby p.Name select p);
    comboBoxExchange.DisplayMemberPath = "Name";
    comboBoxExchange.SelectionChanged += comboBoxExchange_SelectionChanged;
}

So I want to save my data after the user changes the selection with this function:

private void Save()
{
    using (var transaction = m_DbSession.BeginTransaction())
    {
        m_DbSession.SaveOrUpdate(m_DBProduct);
        transaction.Commit();
    }
}

The problem is that I cant find the event that works with this.

  • SelectionChanged is fired when the window is loaded without the user doing anything
  • LostFocus is fired multible times even when the user just "opens" the combobox and closes it without changing anything.
1
can be done without using ComboBox events. there is binding SelectedItem="{Binding Exchange}" - so track changes of Exchange property - ASh

1 Answers

1
votes

SelectionChanged is fired when the window is loaded without the user doing anything

Yes, but you could return from the event handler immediately the first time using the IsLoaded property:

private void comboBoxExchange_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (!IsLoaded)
        return;

    //your code to be executed when the user actually selects an item...
}

So SelectionChanged is the event to handle here.

Alternatively, you could implement kick-off your logic in the setter of the Exchange source property.