How to bind a property of control to another control's property

Hamid Reza

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"));
Sriram Sakthivel
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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to bind a property of control to another control's property

From Dev

How to Bind to Another Control Property in WPF

From Dev

How to bind data to a control's Visibility property

From Dev

How to bind data to a control's Visibility property

From Dev

How to bind a property to the property of another class (with no UI Control)?

From Dev

How to bind a property to the property of another class (with no UI Control)?

From Dev

Bind a property to another property of a custom control

From Dev

How to bind to a control's property which is inside control template?

From Dev

How to bind control to object property?

From Dev

How to bind control to object property?

From Dev

How to bind user control property to other property?

From Dev

How to Bind an another Control on the Setter Value Property by using a Trigger?

From Dev

How to bind custom dependecy property to control's view model?

From Dev

How to programmatically bind a (dependency) property of a control that's inside a DataTemplate?

From Dev

How to bind custom dependecy property to control's view model?

From Dev

How do I bind a WPF control's margin to a property?

From Dev

Bind control to escaped property?

From Dev

How to parse a json object property to bind it to a control

From Dev

How to data-bind control's property within ControlTemplate, to the data-context of the window that contains the control with this template?

From Dev

JavaFX bind not property member to control

From Dev

c# how to modify the control's property

From Dev

Updating a Control's property (label.Text) from another thread

From Dev

Updating a Control's property (label.Text) from another thread

From Dev

Bind to wpf custom control dependency property for tooltip?

From Dev

Bind a custom type property to a custom control

From Dev

bind child user control to class property

From Dev

XAML Bind control property to assigned style

From Dev

Bind WPF user control property inside code

From Dev

Bind flag enums to a control and back to the enum property

Related Related

  1. 1

    How to bind a property of control to another control's property

  2. 2

    How to Bind to Another Control Property in WPF

  3. 3

    How to bind data to a control's Visibility property

  4. 4

    How to bind data to a control's Visibility property

  5. 5

    How to bind a property to the property of another class (with no UI Control)?

  6. 6

    How to bind a property to the property of another class (with no UI Control)?

  7. 7

    Bind a property to another property of a custom control

  8. 8

    How to bind to a control's property which is inside control template?

  9. 9

    How to bind control to object property?

  10. 10

    How to bind control to object property?

  11. 11

    How to bind user control property to other property?

  12. 12

    How to Bind an another Control on the Setter Value Property by using a Trigger?

  13. 13

    How to bind custom dependecy property to control's view model?

  14. 14

    How to programmatically bind a (dependency) property of a control that's inside a DataTemplate?

  15. 15

    How to bind custom dependecy property to control's view model?

  16. 16

    How do I bind a WPF control's margin to a property?

  17. 17

    Bind control to escaped property?

  18. 18

    How to parse a json object property to bind it to a control

  19. 19

    How to data-bind control's property within ControlTemplate, to the data-context of the window that contains the control with this template?

  20. 20

    JavaFX bind not property member to control

  21. 21

    c# how to modify the control's property

  22. 22

    Updating a Control's property (label.Text) from another thread

  23. 23

    Updating a Control's property (label.Text) from another thread

  24. 24

    Bind to wpf custom control dependency property for tooltip?

  25. 25

    Bind a custom type property to a custom control

  26. 26

    bind child user control to class property

  27. 27

    XAML Bind control property to assigned style

  28. 28

    Bind WPF user control property inside code

  29. 29

    Bind flag enums to a control and back to the enum property

HotTag

Archive