I have written a control in C# that derives from System.Windows.Forms.Control. I have added a property Selected to which I want to databind to a business entity using a BindingSource.
I’ve implemented the PropertyNameChanged pattern by adding a SelectedChanged event that I fire when the Selected property is changed.
This is my code:
public partial class RateControl : Control { [Category('Property Changed')] public event EventHandler SelectedChanged; public int Selected { get { return m_selected; } set { if (m_selected != value) { m_selected = value; OnSelectedChanged(); Invalidate(); } } } protected virtual void OnSelectedChanged() { if (this.SelectedChanged != null) this.SelectedChanged(this, new EventArgs()); } }
When I bind to the Selected property, I see the event being subscibed to. The event is also fired when the property changes.
However the business entity is not updated. I don’t even see the getter of the Selected property being accessed.
What am I missing?
Have you got the binding’s update mode set to DataSourceUpdateMode.OnPropertyChanged? Either via
binding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;, or using one of theDataBindings.Add(...)overloads.The following works for me to push values to the business object…