WPF ListView中的WrapPanel不会换行

54
我正在使用一个带有如下ItemTemplate的ListView:
<Window.Resources>
    <DataTemplate x:Key="ItemTemplate">
        <WrapPanel Orientation="Horizontal">
            <Image Width="50" Height="50" Stretch="Fill" Source="{Binding Cover}"/>
            <Label Content="{Binding Title}" />
        </WrapPanel>
    </DataTemplate>
</Window.Resources>

但是封面不能像Windows资源管理器那样填满屏幕。

我该怎么做?在我的版本中,它们只是垂直堆叠。

alt text
(来源:functionx.com)


7
很高兴看到Win98的屏幕.... :) - M.Kumaran
1个回答

131

尝试将WrapPanel作为ListView的项面板,并禁用水平滚动条:

<ListView ScrollViewer.HorizontalScrollBarVisibility="Disabled">
  <ListView.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel Orientation="Horizontal" />
    </ItemsPanelTemplate>
  </ListView.ItemsPanel>
  ...
</ListView>

更新:itowlson建议使用以下解释使事情更加清晰:ItemTemplate指定每个项应如何呈现。它对项的布局没有影响。相比之下,ItemsPanel确实指定了布局。

此外,您可能希望所有项都以相同的大小显示。您可以从这篇文章中了解到如何做到这一点:http://joshsmithonwpf.wordpress.com/2008/09/06/synchronizing-the-width-of-elements-in-an-itemscontrol/


3
我建议您在这种情况下最好使用常规的ListBox而不是ListView。 - Josh
3
仅补充一些解释:ItemTemplate 指定了如何呈现每个项目,但不影响项目的布局。相比之下,ItemsPanel 确定了布局方式。 - itowlson
好的,抱歉我以为它们是相同的WrapPanel。但是现在我这样做,图标水平排列在一行上,而不是像一个正方形网格。 - Joan Venge
4
抱歉,该属性为ScrollViewer.HorizontalScrollBarVisibility-已更新答案。 - Grokys
FYI:在 </ListView.ItemsPanel> 下面,您需要放置 <ListView.ItemTemplate>,然后在 <DataTemplate> 中放置您的实际内容... - CularBytes
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接