c# – How to fill empty space in Grid with TextBlock?

Question:

I have the following XAML

<Window x:Class="ambiente_teste.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ambiente_teste"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel Width="120" Margin="5">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="120"/>
                    <RowDefinition Height="20"/>
                    <RowDefinition Height="30"/>
                </Grid.RowDefinitions>

                <StackPanel Grid.Row="0" Background="#FF38C59F"></StackPanel>
                <TextBlock Grid.Row="1" Text="title" Foreground="LightGray"  VerticalAlignment="Top" TextAlignment="Center"/>
  <!--preencher espaços--> <TextBlock Grid.Row="2" Text="text" Foreground="LightGray" VerticalAlignment="Top" TextAlignment="Center" />
            </Grid>
        </StackPanel>
    </Grid>
</Window>

As you can see, I have spaces left in the third row of the Grid . How can I fill these spaces with TextBlock ?

I want to do similar to Dock.Fill , from WindowsForms .

Answer:

The VerticalAlignment must be in Stretch . Only then will TextBlock fill in the empty spaces.

<Window x:Class="ambiente_teste.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ambiente_teste"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel Width="120" Margin="5">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="120"/>
                    <RowDefinition Height="20"/>
                    <RowDefinition Height="30"/>
                </Grid.RowDefinitions>

                <StackPanel Grid.Row="0" Background="#FF38C59F"></StackPanel>
                <TextBlock Grid.Row="1" Text="title" Foreground="LightGray"  VerticalAlignment="Stretch" TextAlignment="Center"/>
                <!--preencher espaços-->
                <TextBlock Grid.Row="2" Text="text" Foreground="LightGray" VerticalAlignment="Stretch" TextAlignment="Center"/>
            </Grid>
        </StackPanel>
    </Grid>
</Window>
Scroll to Top