JT Smith
JT Smith

Reputation: 381

Extra whitespace appearing around Listview items

I have a grid with a row that has Height="Auto".

In that row I have a listview that is bound to a list showing a string.

When those items appear in the listview they have a bunch of extra whitespace around them (If I set the background of the items there is a narrow band of that color around the text then a bunch of blank space in between the items). I am not setting any margins or padding anywhere, just a basic listview bound to my collection.

How do I get the items to show up snug next to each other instead of having a bunch of whitespace in between them

Here's the relevant XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <ListView ItemsSource="{Binding DisplayItems}" Grid.Row="0" DisplayMemberPath="DisplayString" />

Upvotes: 1

Views: 431

Answers (1)

Justin XL
Justin XL

Reputation: 39006

I just answered a question here which might help you understand what's going on. Following my answer, you can generate the full default style of the ListViewItem.

The following Setters in the style are affecting the spacing of each ListViewItem:

<Setter Property="Padding" Value="11,-1,11,-1" />
<Setter Property="MinWidth" Value="{ThemeResource ListViewItemMinWidth}" />
<Setter Property="MinHeight" Value="{ThemeResource ListViewItemMinHeight}" />

By default, ListViewItemMinWidth is 88 and ListViewItemMinHeight is 44.

You can of course replace them all with 0 in the generated style or, simply override what you need without generating the full style like this

<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="Padding" Value="0" />
            <Setter Property="MinWidth" Value="0" />
            <Setter Property="MinHeight" Value="0" />
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

Upvotes: 6

Related Questions