0

I have two grids in my app, a container grid and a child grid. It is possible for the container grid's width and height to change at runtime. Child grids have a default width and height of 700x700. If the parent grid is 300x300, then the child grid will be 300x300 as well, but when I get the actual width and height of the child grid at runtime, I still get 700x700, despite the fact that the actual width and height are 300x300.

XAML

<Grid Width="300" Height="300">
    <Grid Width="700" Height="700" Loaded="Grid_Loaded" >
    </Grid>
</Grid>

Code Behind

private void Grid_Loaded(object sender, RoutedEventArgs e)
{
    Grid grid = sender as Grid;
    int width = (int)grid.ActualWidth;  //<-- 700, but 300 expected
    int height = (int)grid.ActualHeight;//<-- 700, but 300 expected
}
3SAT
  • 109
  • 3
  • @Clemens As before, I get 700x700 – 3SAT Jul 18 '22 at 08:53
  • 1
    Just do not set a fixed Width and Height. The actual size does not change if you have set a fixed size. – Clemens Jul 18 '22 at 08:56
  • My WPF is a bit rusty, but the inner grid cannot be larger than the outer container can hold. Although you request it be 700x700, it cannot occupy that space so it's actual height is what you see. You'll either have to change the size of the outer grid to accommodate the requested size or put the inner grid in a `ScrollViewer` or other container that can hold it at that size. – Jeff Mercado Jul 18 '22 at 09:37
  • Just don't give them a fixed value or use a value of "Auto" instead. – Ahmed Alayat Jul 18 '22 at 22:37

0 Answers0