如何使WPF ListView项目水平和垂直重复?

时间:2009-10-15 18:27:57

标签: wpf templates xaml listview

我有ListView我希望水平显示的东西。这工作正常,但现在我需要让它们像Windows资源管理器类型一样显示。

例如:

A   B   C

D   E   F

G   H   I

A   B

C   D

E   F

G   H

I

是否可以ListView

2 个答案:

答案 0 :(得分:6)

如果您希望所有商品都具有相同的尺寸,我会选择UniformGrid。它是被忽视的控制之一,在这种情况下可能非常有用。

这就是我制作一个快速而肮脏的工具栏的方法:

<ItemsControl ItemsSource="{Binding}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Command="{Binding}"
                    ToolTip="{Binding Tooltip}">
                <StackPanel Orientation="Vertical">
                    <Image Height="16"
                           Width="16"
                           RenderOptions.BitmapScalingMode="NearestNeighbor"
                           Source="{Binding Image}"
                           HorizontalAlignment="Center" />
                </StackPanel>
            </Button>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Rows="1" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

答案 1 :(得分:3)

听起来你正在寻找WrapPanel。我认为它不适用于ListView,但是如果你想让一个通用项容器使用WrapPanel作为它的布局,你可以使用ItemsControl来完成它并用你想要的任何元素填充它。如下所示:

<ItemsControl>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.Items>
        <TextBlock Margin="20" Padding="20" Text="Blah" Background="#999" />
        <TextBlock Margin="20" Padding="20" Text="Blah" Background="#999" />
        <TextBlock Margin="20" Padding="20" Text="Blah" Background="#999" />
        <TextBlock Margin="20" Padding="20" Text="Blah" Background="#999" />
        <TextBlock Margin="20" Padding="20" Text="Blah" Background="#999" />
        <TextBlock Margin="20" Padding="20" Text="Blah" Background="#999" />
    </ItemsControl.Items>
</ItemsControl>