Tutorial :WPF - UIElement.RenderSize not working for Line shape


I'm trying to make some drawing application and I get strange results in my "selection mode". If I draw a rectangle and "select it" RenderSize returns proper Size for it, but if Line is selected RenderSize returns Size which has Width set as Line.X2, and Height set as Line.Y2. For example: Line begins at X1 = 50, Y1 = 50, ends at X2 = 130, Y2 = 90, RenderSize returns Size with Width = 130 and Height = 90. My selection contains elements of type UIElement so I don't know (and really shouldn't care) what shape is selected in order to make my selection mode as generic as I can and I'd like to draw bounding box while user moves selected shape.

Tried google the problem but found nothing relevant so maybe you could help me with it. Is it because Rectangle has position set by Canvas while Line has its points set explicitly?


The reason you're getting 130x90 is because of the reason you cited. A Rectangle in WPF is position-less, it's just a height/width so the two size values are equal.

However a Line as defined by points necessarily defines a required offset from the origin, and thus the offset is included in the bounding box.

Also note that you can continue to use the Canvas.Top/Left properties with your Line object to further offset it, e.g.:

  <Canvas>      <Line X1="50" X2="130" Y1="50" Y2="90" StrokeThickness="1" Stroke="Blue" Canvas.Top="50" Canvas.Left="30"></Line>    </Canvas>  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »