Reputation: 16280
I have a custom control with a two-way bindable property. I also raise a ValueChanged
event on the value's setter.
For this type of custom control I need to change the DataSourceUpdateMode to OnPropertyChanged
instead of the standard OnValidating
.
As I'm doing all the data-binding within the designer, is it possible that the custom control set's the databinding to OnPropertyChanged
once its added to the form?
EDIT:
Currently I'm doing this:
protected override void OnCreateControl()
{
base.OnCreateControl();
Binding binding = this.DataBindings[0] as Binding;
if (binding != null)
{
binding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
}
}
Is this the correct way or is the OnBindingContextChanged
a more suitable event?
Upvotes: 1
Views: 523
Reputation: 125197
OnCreateControl
is useless and OnBindingContextChanged
is more suitable place to change DataBindings
. Also instead of DataBindings[0]
you should use a loop over DataBindings
and if you want to apply the logic on a specific property, use an if
statement to check PropertyName
of Binding
object:
protected override void OnBindingContextChanged(EventArgs e)
{
foreach (Binding item in DataBindings)
{
if (item.PropertyName == "SomeProperty")
item.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
}
base.OnBindingContextChanged(e);
}
As an idea to apply such custom initialization tasks you can implement ISupportInitialize
interface and put the logic which you want to apply when initialization completes in EndInit
method.
Upvotes: 1