UWP XAML ListView.ItemTemplate does not follow the column spacing defined in ListView.HeaderTemplate

1

I'm writing an app with ListView defined below:

<ListView x:Name="TodosRecibos">
            <ListView.HeaderTemplate>
                <DataTemplate>
                    <Grid Padding="12" Background="Transparent">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="0.75*"/>
                            <ColumnDefinition Width="1.5*"/>
                            <ColumnDefinition Width="3.5*"/>
                            <ColumnDefinition Width="3.75*"/>
                            <ColumnDefinition Width="0.5*"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="Recibo" Style="{ThemeResource BodyTextBlockStyle}" FontWeight="Bold"/>
                        <TextBlock Grid.Column="1" Text="Data" Style="{ThemeResource BodyTextBlockStyle}" FontWeight="Bold"/>
                        <TextBlock Grid.Column="2" Text="Tomador" Style="{ThemeResource BodyTextBlockStyle}" FontWeight="Bold"/>
                        <TextBlock Grid.Column="3" Text="Serviço" Style="{ThemeResource BodyTextBlockStyle}" FontWeight="Bold"/>
                        <TextBlock Grid.Column="4" Text="Valor" Style="{ThemeResource BodyTextBlockStyle}" FontWeight="Bold"/>
                    </Grid>
                </DataTemplate>
            </ListView.HeaderTemplate>
            <ListView.ItemTemplate>
                <DataTemplate x:Name="ListaRecibos" x:DataType="data:Recibo">
                    <Grid Height="48" AutomationProperties.Name="{x:Bind ReciboId}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="0.75*"/>
                            <ColumnDefinition Width="1.5*"/>
                            <ColumnDefinition Width="3.5*"/>
                            <ColumnDefinition Width="3.75*"/>
                            <ColumnDefinition Width="0.5*"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock VerticalAlignment="Center" Text="{x:Bind ReciboId}"/>
                        <TextBlock Grid.Column="1" VerticalAlignment="Center" Text="{x:Bind ServicoData}"/>
                        <TextBlock Grid.Column="2" VerticalAlignment="Center" Text="{x:Bind TomadorNome}"/>
                        <TextBlock Grid.Column="3" VerticalAlignment="Center" Text="{x:Bind ServicoDescricao}"/>
                        <TextBlock Grid.Column="4" VerticalAlignment="Center" HorizontalAlignment="Right" Text="{x:Bind ServicoValor}"/>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

I expected items in ListView to follow column spacing set to ListView.HeaderTemplate , identical to ListView.ItemsTemplate . However, when executing, the items look like this:

How can I make items in ListView correctly align to columns?

    
asked by anonymous 15.06.2018 / 21:26

1 answer

1

In this StackOverflow Question there is a solution:

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    </Style>
</ListView.ItemContainerStyle>

The respondent explains that, by default, ListViewITem does not stretch its contents, which is achieved with the above XAML code.

    
15.06.2018 / 22:38