BrianKE
BrianKE

Reputation: 4185

WPF ComboBox changing when it shouldn't

I have a ComboBox of Employers. Upon selecting an Employer, a table is populated with Employer specific data:

        <ComboBox Name="EmployerListBox"
                  IsReadOnly="True"
                  ItemsSource="{Binding EmployerCollection, UpdateSourceTrigger=PropertyChanged}"
                  SelectedItem="{Binding SelectedEmployer, UpdateSourceTrigger=PropertyChanged}" 
                  Width="150" />

Here is the property to which it is binding and a method that checks to see if the table is dirty. If the table is dirty, then the user is prompted that changes will be lost if they change Employer:

    /// <summary>
    /// Selected Employer
    /// </summary>
    public String SelectedEmployer
    {
        get
        {
            return _SelectedEmployer;
        }
        set
        {
            if (_SelectedEmployer != value && CanChangeEmployer())
            {
                _SelectedEmployer = value;
                NotifyPropertyChanged(m => m.SelectedEmployer);
                GetGarnishmentsTableView();
            }
        }
    }
    private String _SelectedEmployer = "";


    /// <summary>
    /// Method that executes each time user wants to change employers
    /// </summary>
    public Boolean CanChangeEmployer()
    {
        Boolean _returnValue = true;

        if (GarnishmentsTableIsDirty)
        {
            _returnValue = false;

            MessageBoxResult _change =
                MessageBox.Show("There are unsaved changes.  " +
                                "Changing Employers will lose any unsaved changes.  \n\n" +
                                "Are you sure you want to change Employers?", "Unsaved Changes", MessageBoxButton.YesNo);
            if (_change == MessageBoxResult.Yes)
            {
                // OK to switch employers
                _returnValue = true;
            }
        }

        return _returnValue;
    }

Everything appears to work correctly:

Yet back in the GUI the Employer option changes to the Employer the user selected ('MPC'), even though SelectedEmployer hasn't changed.

When I Snoop the ComboBox, I see that that ComboBox SelectedItem is correctly set to the original Employer ('KMH') but SelectedValue and SelectionBoxItem are both set to the new Employer ('MPC').

I then tried binding ComboBox->SelectedValue to SelectedEmployer:

        <ComboBox Name="EmployerListBox"
                  IsReadOnly="True"
                  ItemsSource="{Binding EmployerCollection, UpdateSourceTrigger=PropertyChanged}"
                  SelectedValue="{Binding SelectedEmployer, UpdateSourceTrigger=PropertyChanged}" 
                  Width="150" />

And the GUI is the same, incorrect company being displayed. But this time Snoop shows that ComboBox->SelectedValue is correctly set to the original Employer ('KMH') but SelectedItem and SelectionBoxItem are both set to the new Employer ('MPC').

How do I correctly bind SelectedEmployer so that the GUI matches the selected employer?

Upvotes: 2

Views: 376

Answers (0)

Related Questions