I have a custom control that has a Value
property that supports two-way binding and also a ValueChanged
event:
public event EventHandler ValueChanged;
[Bindable(BindableSupport.Yes, BindingDirection.TwoWay)]
public virtual MyObject Value
{
get { return this.value; }
set
{
this.value = value;
OnValueChanged(new EventArgs());
}
}
private void OnValueChanged(EventArgs e)
{
EventHandler handler = ValueChanged;
if (handler != null)
handler(this, e);
}
This control works fine when placed on a form and the data bindings set in the designer. The bindingsource subscribes to the event correctly and the underlying datasource gets updated accordingly.
Now, I'm creating the control dynamically by doing the following:
MyControl ctl = new MyControl();
ctl.DataBindings.Add(new Binding("Value", this.bindingSource, "SomeField", true, DataSourceUpdateMode.OnPropertyChanged));
However, by doing this, the bindingsource does not subscribe to the ValueChanged
event. I checked the code generated by the designer, and there is not that makes the bindingsource register to event. I assume that adding the binding to the data bindings should do this, but it doesn't.
What could be missing here?