简体   繁体   中英

Align GridView children to top of cell in UWP XAML

I am using GridView to display a grid of variable-height items, except each cell is aligned to the middle of the row.

在此处输入图像描述

My markup is

<GridView ItemsSource="{x:Bind Bounties}" SelectionMode="None" HorizontalAlignment="Left"
          VerticalAlignment="Top" >
  <GridView.ItemTemplate>
    <DataTemplate x:DataType="local:Bounty">
      <Grid>
        <TextBlock Margin="12" MaxWidth="350" HorizontalAlignment="Left"
                   Text="{x:Bind ItemDefinition.DisplayProperties.Description}"
                   Style="{ThemeResource BodyTextBlockStyle}" TextWrapping="WrapWholeWords" />
      </Grid>
    </DataTemplate>
  </GridView.ItemTemplate>
</GridView>

How can I get each cell to align to the top left of its row?

The internal content of the GridViewItem is vertically centered, this is the default style, if you want to modify it, please try this:

<GridView ...>
    <GridView.ItemContainerStyle>
        <Style TargetType="GridViewItem">
            <Setter Property="VerticalContentAlignment" Value="Top" />
        </Style>
    </GridView.ItemContainerStyle>
    <GridView.ItemTemplate>
        ...
    </GridView.ItemTemplate>
</GridView>

In addition, if you want to make the elements in the GridView adaptive height (the default is the height of the first element or the preset height as the height of all items), you can try StaggeredPanel in Windows Community Toolkit.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM