Reputation: 60
I have Control
in which I bind my own class Element
with "DataBindings". If I change "Width" and "Height", for example, in "E", Control change the same properties as well. But other side it don't work.
this.DataBindings.Add("Width", E, "Width");
this.DataBindings.Add("Height", E, "Height");
What is the best way to fix it? Only by hands, problem that there is many properties? Or exists something like "DataBindings"?
P.S. Element
not inherited from any class, he haven't "DataBindings".
Thanks!
Upvotes: 0
Views: 138
Reputation: 73253
Whan adding your bindings, use one of the overloads which allow you to to set the
DataSourceUpdateMode
and set it to DataSourceUpdateMode.OnPropertyChanged
Upvotes: 0
Reputation: 81620
I don't think a control will report width and height property changes to the databinding listeners.
Try adding the INotifyPropertyChanged
to the control and take over the Width
and Height
properties yourself.
An example using a Panel
control:
public class PanelEx : Panel, INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
public new int Width {
get { return base.Width; }
set {
base.Width = value;
OnPropertyChanged("Width");
}
}
public new int Height {
get { return base.Height; }
set {
base.Height = value;
OnPropertyChanged("Height");
}
}
private void OnPropertyChanged(string propertyName) {
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Then change your databinding call to this, where this
is your control with the above implementation:
this.DataBindings.Add("Width", E, "Width", false, DataSourceUpdateMode.OnPropertyChanged);
this.DataBindings.Add("Height", E, "Height", false, DataSourceUpdateMode.OnPropertyChanged);
Upvotes: 0
Reputation: 956
You should implement INotifyPropertyChanged
in your class E
for every property you want to bind.
Other way around won't work. Control
has to have *Changed event for every property that needs to update datasource. For your example, you can try with Size
property of the control, because there is a SizeChanged
event.
Upvotes: 0
Reputation: 23103
To get it working both ways the following must be present:
INotifyPropertyChanged
Mode
of the Binding must be TwoWay
EDIT: Just saw you are using WinForms - I'm not shure it works there the same way!
Upvotes: 2