views:

352

answers:

1

Hello,

I'm writing a WinForms app, and am trying to bind a boolean property on a .NET object to a Checkbox's "checked" property. I am successfully creating the binding, but when I change the source property's value from false to true (I have a button that toggles it), the checkbox's "checked" property does not reflect that change.

if (chkPreRun.DataBindings["Checked"] == null)
{
    Debug.WriteLine("Adding chkPreRun databinding");
    Binding _binding = chkPreRun.DataBindings.Add("Checked", NwmConfig, "PreRun")

    // Added this just to ensure that these were being set properly
    _binding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
    _binding.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;
}

I am able to successfully bind the text property to the value of a TextBox, for example. I'm not sure what I'm missing while binding to the "Checked" property, however.

Cheers,

Trevor

+2  A: 

For this to work, the source must have either a PreRunChanged event (EventHandler) that is getting fired, or it must implement INotifyPropertyChanged (including for this property). Or as an edge-case, must have a custom PropertyDescriptor implementation that supports notification (but that is very rare.

Does your code have a PreRunChanged ? Does it get raised at the appropriate time?

(the UI doesn't poll for changes; it only knows about changes via the notification events)

Marc Gravell
Marc,I do not have the event handler you mentioned, nor do I implement INotifyPropertyChanged. Your post at least points me in the right direction.I should probably do some more reading up on DataBinding.Thanks for the answer.-Trevor
Trevor Sullivan
Looks like INotifyPropertyChanged will do exactly what I need it to.
Trevor Sullivan