<...">

C # WPF Limit Positions in a String in Listview

My list:

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

List data table:

 <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 the elements are displayed in one line, my question is: how can I show only 3 elements in a line instead of all elements in one line.

Thank you for attention.

+4
source share
3 answers

Using

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

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

+10
source

Use a WrapPanel instead of a StackPanel . It does not allow you to directly indicate the number of elements in a row, but you can set the width of each element, which is almost as good. When there is no space in a line, it continues in the next line.

EDIT: You can also use UniformGrid as suggested by Bonial. The downside is that if you can resize the user interface and make the ListView wider, the number of elements in the row will not change and they will be stretched to fill the gap. Depending on what you want, this might be OK, but I think WrapPanel is the best option in most cases.

+1
source

You want to replace this ListView.ItemsPanel StackPanel WrapPanel . He will do his job. After the WrapPanel has a width, it will wrap the lines with elements.

+1
source

Source: https://habr.com/ru/post/1333238/


All Articles