Home > Net >  WPF How to access nested grid?
WPF How to access nested grid?

Time:05-02

How to access a nested grid element with Grid.Column and Grid.Row? Normally, Grid.Column="1" Grid.Row="0" is the way to go. But I dont know how to access a nested one? for example: Column 0, Nested Column 3, Row 1.

I have added a picture of the layout structure: Grid Layout

<Button Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center"  VerticalAlignment="Top" Width="87" Height="23" Text="btn" BorderThickness="1,1,0,1"  />

Nested grid:

<Grid x:Name="LayoutRoot" Background="#FFA3A3A3" Height="540" Width="952" HorizontalAlignment="Left" VerticalAlignment="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="10"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="10"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="10"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="10"/>
            </Grid.RowDefinitions>
            <Grid Grid.Column="1" Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="140*"/>
                    <ColumnDefinition Width="346*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="52*"/>
                        <RowDefinition Height="220.5*"/>
                    </Grid.RowDefinitions>
                </Grid>
            </Grid>
            <Grid Grid.Column="1" Grid.Row="2">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="40*"/>
                    <ColumnDefinition Width="120*"/>
                    <ColumnDefinition Width="146*"/>
                    <ColumnDefinition Width="146*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

            </Grid>

                Some code...

                <Button Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center"  VerticalAlignment="Top" Width="87" Height="23" Text="btn" BorderThickness="1,1,0,1"  />

</Grid>

CodePudding user response:

you can/need not access a nested grid. Grid.Column/Row always means the parent grid.

<Grid x:Name="Grid1">
    <Grid x:Name="Grid2" Grid.Row="0", Grid.Columns="0">
        <TextBlock Grid.Row="0", Grid.Columns="0"> TextBlock in Grid2[0,0]</TextBlock>
    </Grid>
    <TextBlock Grid.Row="1", Grid.Columns="0"> TextBlock in Grid1[1,0]</TextBlock>
</Grid>

The nested Grid2 is in [0,0] of Grid1

  • Related