ActualWidth and ActualHeight of custom control in grid are zero, even after measure/arrange

738 Views Asked by At

I have a Win8 app with a custom control, GameView, which I have instantiated three times (as gvMap, gvOurBase, and gvEnemyBase) on my page, and placed into a grid. I need to get the actual size of the control so I can set the RenderTransform and render everything with the proper scaling.

However, whenever I attempt to get the ActualWidth or ActualHeight of my controls, I always get zero. This occurs even if I wait until the page is loaded and call Measure and Arrange on my controls.

How can I get the size of my controls? Or am I not allowed to do this while they are in a grid? Is there another way for me to do the RenderTransform that doesn't rely on the size of the controls? I want each control to show a specific section of the whole game map - gvMap shows the whole map, gvOurBase shows the section of the map that the player has cities, and gvEnemyBase shows the section of the map that the enemy has cities.

2

There are 2 best solutions below

2
Jerry Nixon On

That can happen. I recommend MyControl.RenderSize.Width instead of MyControl.ActualWidth. In layouts that result from dynamic data binding, Actual values aren't always populated.

1
ekolis On

Apparently I needed to actually override MeasureOverride on my custom control class. Oops!