0

I have a class that extends a Canvas. On creation I explicitly set its width to a value:

Public Sub New(w As Integer, h As Integer)
    Me.Width = w
    Me.Height = h
End Sub

Now I add instances of this to a canvas to another canvas using cnvPreview.Children.Add(e) where e is the instance of my extended canvas.

However when I run the application, the ActualWidth and ActualHeight of these canvases are 0.

How can this be fixed?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
blaffie
  • 505
  • 1
  • 10
  • 32
  • When are you reading the `ActualWidth` and `ActualHight`? – Emond Dec 20 '12 at 09:38
  • Just after I added the children to the canvas, I used a messagebox to debug. – blaffie Dec 20 '12 at 09:41
  • 1
    Don't canvas's shrink to the smallest size possible, yet still display fully their own children? Might this be causing the `ActualHeight` and `ActualWidth` values to be zero. Try adding say, a `TextBlock` and reading the values then. – JosephGarrone Dec 20 '12 at 09:47
  • @Asryael This seems to be the case, when only the background colour is changed, I can see the canvas occupies the area I specified but the `actualHeight` and `actualWidth` is still zero. If a component is added the desired size is shown. Thanx. – blaffie Dec 20 '12 at 09:51
  • @Blaffie Added comment as answer. Please mark as answer if it is! – JosephGarrone Dec 20 '12 at 09:56

2 Answers2

1

Don't Canvas's shrink to the smallest size possible, yet still display fully their own children? Might this be causing the ActualHeight and ActualWidth values to be zero. Try adding say, a TextBlock and reading the values then.

I believe that this is explained better on this other post: Why are ActualWidth and ActualHeight 0.0 in this case?

Community
  • 1
  • 1
JosephGarrone
  • 4,081
  • 3
  • 38
  • 61
0

Note that ActualHeight and ActualWidth will give you the values after rendering the control. Suppose if the Canvas Height and Width is 50 and after rendering the Canvas is shrinked then it will return a lower ActualHeight and ActualWidth value or maybe zero if not visible.

SidPen
  • 182
  • 1
  • 14