3
votes

I have the following canvas :

<Canvas Width="800" Height="600">
    <Rectangle Fill="#FF333333" Height="124" Width="500" 
               Canvas.Bottom="0" />
    <Rectangle Fill="#FF333333" Height="124" Width="500"
               Canvas.Bottom="150" Canvas.Left="150"/>
    <Rectangle Fill="#FF333333" Height="124" Width="500" 
               Canvas.Bottom="300"/>
</Canvas>

I would like to get the size (width and height) of the rectangular space occupied by the group of rectangles. In the example above, I am expecting these values :

ContentWidth = 650 (= 500 + 150)
ContentHeight = 424 (= 300 + 124)

ActualWidth and ActualHeight return respectively 800 and 600, which is not what I want. Any idea?

1
First thing that comes to mind is simply looping through Canvas.Children, calculating the 4 sides, and creating a rectangle out of them. I'm not sure if there's an easier way or not, as Canvas typically does not care what children it contains, so may not have anything to monitor the size of them. - Rachel
That would be a manual approach. Not really what I am looking for. It might get annoying (I am lazy too ;) ) as I will probably add more shapes like rotated triangle, lines, etc. I wish I could get the size of the "drawn zone". Is there not a more automatic way of doing it? Like using a particular property? - user3240131

1 Answers

1
votes

I'm afraid that this that you want, need to be calculated manually. The thing is that the Canvas is a panel that has not DesiredHeight and DesiredWidth, that means that it doesn't take in count the width/height of children for suggesting a desired size for it.

You could calculate it, like :Rachel said, or you may create a new panel (some thing like CustomCanvas) that has the rectangle properties that you want.

Hope this tips helps...