↓ Skip to main content
開発メモ・逆引き

Split the UI Using a Grid in Avalonia

Environment: Avalonia 12 / .NET 10

In XAML
#

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="YourApp.MainWindow">

    <Grid RowDefinitions="*,*"
          ColumnDefinitions="*,*">

        <TextBlock Grid.Row="0" Grid.Column="0"
                   Text="Top Left" />

        <TextBlock Grid.Row="0" Grid.Column="1"
                   Text="Top Right" />

        <TextBlock Grid.Row="1" Grid.Column="0"
                   Text="Bottom Left" />

        <TextBlock Grid.Row="1" Grid.Column="1"
                   Text="Bottom Right" />

    </Grid>
</Window>

Use RowDefinitions to specify the number and heights of rows, and ColumnDefinitions to specify the number and widths of columns.

Set Grid.Row and Grid.Column on each control to specify its position.

Row heights and column widths can be specified using the following values:

  • *: Fills the remaining available space
  • Auto: Sizes to fit the content
  • Number (fixed size): Sets a fixed size

To add more rows or columns or customize their sizes
#

Add comma-separated values to RowDefinitions and ColumnDefinitions to increase the number of rows and columns.

<Grid RowDefinitions="*,*,*"
      ColumnDefinitions="*,*,*">

Alternatively, you can define each row and column separately, as shown below.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
</Grid>