Vb.net – Binding Custom object to Checkbox in VB.NET

data-bindingvb.netwinforms

I am trying to bind a checkbox to a custom object boolean property as follows:

chkTableIsReadonly.DataBindings.Add(New Binding("Checked", objectBindingSource, "ApplyforVisa", True, DataSourceUpdateMode.OnPropertyChanged, False))

The custom class supports the INotifyPropertyChanged interface.

Everything works find when I initially bind the checkbox to a new object:

objectBindingSource.Datasource = new objectToBindTo

Here is the odd part:

  1. If I check the box, the property Set gets called and the INotifyPropertyChanged event gets called and everyone is happy.
  2. If I uncheck the same box, the property Set doesn't get called, the INotifyPropertyChanged event never gets called and (the worse part), I cannot navigate to another record.

I have tried capturing the CheckedChanged event to set the object.ApplyForVisa property manually, but no success. The property Set gets called and the INotifyPropertyChanged event gets called, but I am still locked on control and can't navigate.

I have tried calling bindingsource.endedit in the CheckedChanged event, no success.

It only matters if I uncheck the box. The checkbox is two-state – true or false.

All of my other bindings work just fine – text boxes, combo boxes, datagrid. Just not checkbox.

My only thought is that is seems to act like a binding source data error, but no error is thrown. If I add the data error event handler for the binding source, it never gets called.

Best Answer

Assuming the ApplyForVisa property is a Boolean, you can just fix this by setting formattingEnabled parameter for the Binding to False.

chkTableIsReadonly.DataBindings.Add( _
    New Binding("Checked", objectBindingSource, "ApplyforVisa", _
                False, DataSourceUpdateMode.OnPropertyChanged, False))
Related Topic