user3240131
user3240131

Reputation: 207

How to get canvas content size in wpf?

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?

Upvotes: 3

Views: 1304

Answers (1)

Ra&#250;l Ota&#241;o
Ra&#250;l Ota&#241;o

Reputation: 4760

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...

Upvotes: 1

Related Questions