WinForms (C#) Databinding Object to Checkbox.Checked Property
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.OnProp开发者_如何学运维ertyChanged;
_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.
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)
精彩评论