Evaluate UserControl Position and Width / Height Rendering

I have a UserControl (boxes) that can have a different size depending on the number of items in the ItemsControl . Many of these user controls are programmatically added to the Canvas .

I need to draw arrows connecting these usercontrols. What is the best way to get the starting coordinates of the wrt Canvas control and the displayed Width / Height so that I can figure out the start and end points of the arrow.

0
source share
1 answer

Canvas provides the coordinates of each control with Canvas.Left and Canvas.Top attached properties, which you know if you yourself positioned them yourself. This way, the (slightly) more complex part gets a different coordinate, and for that you want to know the height / width of the rendering. ActualHeight and ActualWidth give you this, assuming the control is already laid out:

 double top = Canvas.GetTop(control) double bottom = top + control.ActualHeight double left = Canvas.GetLeft(control) double right = left + control.ActualWidth 

If you do this before the controls were able to display on the screen, you can first do control.UpdateLayout() (or control.Measure() ) so that the layout system estimates their size.

+2
source

Source: https://habr.com/ru/post/904659/


All Articles