I have two controls on my form(control1 and control2) that are located next to the each other. The width of control1 is variable.
Can I bind the Left Property of control2 to the width property of control1?
I wrote below code but it didn't work:
control2.DataBindings.Add(new Binding("Left", control1, "Width"));
control2.DataBindings.Add(new Binding("Left", control1, "Width"));//Your code
Your code doesn't work because there is no change notification going when chaning Width
property, but Size
does.
You can bind to Size
but the problem is you need an int
but Size property is of type Size
, so you need to convert it also using Format event like this.
var binding = new Binding("Left", control1, "Size", true,DataSourceUpdateMode.Never);
binding.Format += (sender, args) =>
{
if (args.DesiredType == typeof (int))
{
Size size = (Size) args.Value;
args.Value = size.Width;
}
};
control2.DataBindings.Add(binding);
Another way is to implement INotifyPropertyChanged
in your source control. That should do the trick.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments