I’m trying to get the width and the height of a grid programmatically, In a UserControl.
Here is my XAML grid:
<Grid Name="BallGrid" Background="DarkBlue" Height="auto" Width="auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
</Grid>
But both of these:
double width = BallGrid.ColumnDefinitions[0].ActualWidth;
double height = BallGrid.RowDefinitions[0].ActualHeight;
return 0,
and these
double width = BallGrid.ColumnDefinitions[0].Width.Value;
double height = BallGrid.RowDefinitions[0].Height.Value;
return both return 1.
I’d like to know the actual width, preferably in the same units as margin sizes. How can I get these values programmatically?
The column values are queried in a ProgessChanged method, that is called by a BackgroundWorker every 50 ms, maybe that makes a difference.
Usually you use
ActualWidth/Height, here those properties are a bit lazy:So you could probably call
Measuremanually or try to wait for it.