Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# WPF Limit items per row in a Listview

My Listview:

<ListView ItemTemplate="{StaticResource GridViewItemTemplate}" Name="gridView_movies">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left"  VerticalAlignment="Top"/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>

Datatemplate of the list:

<DataTemplate x:Key="GridViewItemTemplate">
    <StackPanel Orientation="Vertical" >
        <Image Width="250" Height="290" Source="{Binding Image}"/>
        <TextBlock Text="{Binding Title}" HorizontalAlignment="Center"  VerticalAlignment="Top" FontSize="20"/>
    </StackPanel>
</DataTemplate>

When i load this, all items are showed in one row, my question is, How can i show only 3 items per row instead of all items in one row.

Thanks for the attention.

like image 656
David Pires Avatar asked Dec 10 '22 12:12

David Pires


2 Answers

Use

<UniformGrid Columns="3" .../>

instead of <StackPanel Orientation="Horizontal" .../> in ItemsPanelTemplate

like image 86
Mykola Bogdiuk Avatar answered Dec 22 '22 21:12

Mykola Bogdiuk


Use a WrapPanel instead of a StackPanel. It doesn't allow you to directly specify the number of items per row, but you can set the width of each item, which is almost as good. When there is no space left on a row, it continues on the next row.

EDIT: you could also use a UniformGrid, as suggested by Bonial. The drawback is that if you can resize your UI and make the ListView wider, the number of items per row won't change, and they will be stretched to fill the space. Depending on what you want, it might be OK, but I think WrapPanel is a better option in most cases.

like image 32
Thomas Levesque Avatar answered Dec 22 '22 20:12

Thomas Levesque