質問

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?

役に立ちましたか?

解決

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?

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top