wpflayoutactualwidthactualheight

Why would actual width be zero when width is set to a value. WPF


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?


Solution

  • 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?