How does WPF identify attached properties when multiple same-level containers are present?

StackOverflow https://stackoverflow.com/questions/15451527

  •  24-03-2022
  •  | 
  •  

Question

I am fairly new to WPF and have a fundamental question regarding attached properties. Consider the following code:

    Canvas.SetLeft(myRectangle, 20);
    Canvas.SetTop(myRectangle, 40);

I understand that the static methods of the Canvas class are being invoked above. What I don't understand is this: what if the application's main window contained two canvas containers, side by side, i.e. at the same level in the hierarchy? In that case, how does the WPF runtime not confuse which canvas's attached properties are being referenced?

Note: I have not tried this myself, as I have not been able to position two canvas elements side by side in the main window.

Was it helpful?

Solution

The attached property being set isn't specific to a single Canvas instance. It is simply setting a property which is known to Canvas on a specific control. All Canvas instances are free to access this property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top