C# WPF 限制列表视图中每行的项目
我的列表视图:
<ListView ItemTemplate="{StaticResource GridViewItemTemplate}" Name="gridView_movies">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
列表的数据模板:
<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>
当我加载此视图时,所有项目都显示在一行中,我的问题是,如何每行仅显示 3 个项目而不是一行中的所有项目。
感谢您的关注。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
ItemsPanelTemplate
中使用而不是
Use
instead of
<StackPanel Orientation="Horizontal" .../>
inItemsPanelTemplate
使用
WrapPanel
而不是StackPanel
。它不允许您直接指定每行的项目数,但您可以设置每个项目的宽度,这几乎一样好。当一行中没有剩余空间时,它将继续下一行。编辑:您也可以使用
UniformGrid
,正如 Bonial 所建议的。缺点是,如果您可以调整 UI 大小并使ListView
更宽,则每行的项目数不会改变,并且它们将被拉伸以填充空间。根据您的需要,这可能没问题,但我认为在大多数情况下WrapPanel
是更好的选择。Use a
WrapPanel
instead of aStackPanel
. 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 theListView
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 thinkWrapPanel
is a better option in most cases.您想要将 ListView.ItemsPanel StackPanel 替换为 WrapPanel< /a>.它会完成工作。一旦 WrapPanel 具有宽度,它将对项目进行换行。
You want to replace that ListView.ItemsPanel StackPanel with a WrapPanel. It will get the job done. Once the WrapPanel has a width, it will then line wrap the items.