Ivan
Ivan

Reputation: 1171

How to remove the spacing between few ListView items?

If I have only few elements (pictures) in my ListView, then they get equally spaced (one at the top, other one in the middle and third one at the bottom). This however leaves lots of space between the itmes. I would like them to be arranged vertically at the top of the list, like in PowerPoint thumbnail view. I have tried the following propetries on list items, but none of them worked:

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="Background" Value="Pink" />
        <Setter Property="Padding" Value="0" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="HorizontalAlignment" Value="Center" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
    </Style>

</ListView.ItemContainerStyle>

I managed to remove the padding and to adjust the height and the width, but items still stay equally spaced across the list.

I would appreciate some help.

Upvotes: 1

Views: 3240

Answers (1)

Slime recipe
Slime recipe

Reputation: 2263

You can implement your own template for the listview items.

In theory it may look similar to this:

<ListView x:Name="myListview">
 <ListView.ItemTemplate>
    <DataTemplate>
      <Image Source="{Binding}" Margin="0" />
   </DataTemplate>
 </ListView.ItemTemplate>
</ListView>

That way you have your items represented by images with 0 Margin which is what I think causes the separation.

I haven't tested it in Visual Studio so let me know if there are any problems.

Upvotes: 1

Related Questions