我希望在ListView中以类似于列表模式中的WinForms ListView的方式来布局项目。也就是说,项目在ListView中不仅是垂直布局的,而且是水平布局的。
我不介意项目的布局是这样的:
1 4 7
2 5 8
3 6 9
或者像这样:
1 2 3
4 5 6
7 8 9
只要它们是垂直和水平呈现的,以便最大限度地利用可用空间。
我能找到的最接近的问题是:
How do I make WPF ListView items repeat horizontally, like a horizontal scrollbar?
它只对项目进行水平布局。
发布于 2009-06-25 01:38:54
听起来您正在寻找的是一个WrapPannel,它将水平放置项目,直到没有更多的空间,然后移动到下一行,如下所示:
(MSDN)
alt text http://i.msdn.microsoft.com/Cc295081.b1c415fb-9a32-4a18-aa0b-308fca994ac9(en-us,Expression.10).png
您还可以使用UniformGrid,它将在设置的行数或列数中布局项目。
在ListView、ListBox或任何形式的ItemsControl中使用这些其他面板排列项的方法是通过更改ItemsPanel属性。通过设置ItemsPanel,您可以更改ItemsControls使用的默认StackPanel。对于WrapPanel,我们还应该将宽度设置为shown here。
<ListView>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="{Binding (FrameworkElement.ActualWidth),
RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
ItemWidth="{Binding (ListView.View).ItemWidth,
RelativeSource={RelativeSource AncestorType=ListView}}"
MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}"
ItemHeight="{Binding (ListView.View).ItemHeight,
RelativeSource={RelativeSource AncestorType=ListView}}" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
...
</ListView>发布于 2009-06-25 06:59:45
我最近研究了如何在WPF中实现这一点,并找到了一个很好的解决方案。我想要的是在Windows资源管理器中复制列表模式,即从上到下,然后从左到右。
基本上,您要做的是覆盖ListBox.ItemsPanel属性,以使用方向设置为垂直的WrapPanel。
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>然而,当加载一个大型数据集时,这个将很慢,因为它的包装面板没有虚拟化。这事很重要。因此,这项任务现在变得更复杂了,因为现在您需要通过扩展VirtualizedPanel和实现IScrollInfo来编写自己的VirtualizedWrapPanel。
public class VirtualizedWrapPanel : VirtualizedPanel, IScrollInfo
{
// ...
}这就是我在不得不去做另一个任务之前所做的研究。如果你想要更多的信息或例子,请评论。
更新。本·康斯特布尔的series on how to implement IScrollInfo很棒。
全文共4篇。这是一本非常好的书。
我已经实现了一个虚拟化的包装面板,即使在上述系列文章的帮助下,这也不是一件容易的事情。
发布于 2012-05-04 22:01:45
在我的例子中,最好的选择是使用:
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"
MaxHeight="{Binding (FrameworkElement.ActualHeight), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
ItemWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}"
MinHeight="{Binding ItemHeight, RelativeSource={RelativeSource Self}}"
ItemHeight="{Binding (ListView.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListView}}"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>这使我可以很好地模拟Windows资源管理器的列表选项
https://stackoverflow.com/questions/1041551
复制相似问题