Binding ComboBox to ViewModel in WinForms

1k Views Asked by At

I have a form containing two ComboBoxes, each bound to a BindingSource. The bindingSource of box A has a BindingList<MyObject> as DataSource and box B a BindingList<string>.

The SelectedItem property of both ComboBoxes are bound to properties on a view model (let's call them MyObject PropA and string PropB) implementing INotifyPropertyChanged.

I'm having two issues at the moment:

  1. When populating the ComboBoxes, the bindings to the view model are not being updated and the properties stay empty, until I change the selection in the ComboBox. I'd like the view model to be updated right away, in case the user doesn't change the selection. Is this somehow possible, maybe by overriding ComboBox?

  2. If I allow user input in ComboBox B (string), select an item in the box and then edit it and click on another control, the property on the view model becomes null and the text in the ComboBox changes back. When doing it again right away on the same selected item, the edited text stays, but the property is still null. If I just select an item without editing, the binding seems to work fine.

Is there something wrong with the ComboBox control in regards to two-way binding, or am I just using them wrong?

Thanks for your feedback!

EDIT

ComboBox A (productTypesComboBox):

// productTypesBindingSource
// 
this.productTypesBindingSource.AllowNew = false;
this.productTypesBindingSource.DataMember = "ProductTypes";
this.productTypesBindingSource.DataSource = this.productDirectoryPresModelBindingSource;
// 
// productTypesComboBox
// 
this.productTypesComboBox.DataBindings.Add(new System.Windows.Forms.Binding("SelectedItem", this.productBindingSource, "ProductType", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
this.productTypesComboBox.DataSource = this.productTypesBindingSource;
this.productTypesComboBox.DisplayMember = "Name";

ComboBox B (unitsComboBox):

// unitsBindingSource
// 
this.unitsBindingSource.DataMember = "Units";
this.unitsBindingSource.DataSource = this.productDirectoryPresModelBindingSource;
// 
// unitsComboBox
// 
this.unitsComboBox.DataBindings.Add(new System.Windows.Forms.Binding("SelectedItem", this.productBindingSource, "Unit", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
this.unitsComboBox.DataSource = this.unitsBindingSource;

ViewModel:

// 
// productBindingSource
// 
this.productBindingSource.DataSource = typeof(SiMA.ViewModels.ProductVM);

Behind the view:

private void BindToPresentationModel()
{
    presentationModel = new ProductDirectoryPresModel();
    presentationModel.ItemsBindingSource = productBindingSource;

Presentation Model (when some button is clicked)

public virtual void AddNewItem()
{
    if (!currentItemIsSaved)
    {
        DialogResult result = ShowSaveChangesDialog();
        if (result == DialogResult.Yes)
        {
            SaveCurrentItem();
            ItemsBindingSource.AddNew();
        }
    ...
0

There are 0 best solutions below