views:

739

answers:

1

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?

+1  A: 

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>
micahtan
So, is there any way to generally retrieve proper Size (or something like it) of the UIElement and display bounding box based on it? I don't like the idea of special testing and treatment for Line (or whatever other Shape which has the same problem)
grapkulec
Is there a particular reason you're not using 0,0 as X1,Y1? You would have the correct bounding box if this was the case, and use Canvas.Top/Canvas.Left the same way that you place your Rectangle objects.
micahtan
So you propose using 0,0 as one of the line's points and instead of changing that point's coordinates move line by Canvas.SetLeft / Canvas.SetTop methodes and second point? Actually that might work, thx :) But still there should be more intuitive or elegant way of doing that.
grapkulec
The design decision the WPF team made makes sense when you think about the Line as a graphical object that includes it's offset from the origin in addition to the line itself. Otherwise, you could just define it using a single point (X,Y) and assume 0,0 to be the other, and force users of the API to use Top/Left offsets.
micahtan
Ok, I think I can manage to live with that :)Thx for explanation!
grapkulec