views:

360

answers:

2

Hi,

I have a listview where I would like to display things horizontally. That works fine, but now I would need to have them display like in a Windows Explorer type.

For example :

A B C

D E F

G H I

or

A B

C D

E F

G H

I

Is it possible in a listview?

+1  A: 

Sounds like you're looking for WrapPanel. I don't think it works for ListView, but if you want a generic items container to use WrapPanel as it's layout, you can do this with ItemsControl and fill it with whatever elements you want. Something like the following:

<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>
Rich
Like you said it doesn't seem to work with WrapPanel, but what I will do is have two list that are on top of one another, since I know the exact number of max item I will have.
David Brunelle
You can get this to work with ListView. I've done it, but don't have the code handy at the moment. Do some Google searches, and you'll find it.
John Fisher
Does it "have" to be a ListView? WPF is so flexible, that you can get a lot of the same list displaying functionality out of a number of different containers. The container isn't usually the functionality you're looking for, it's the templates (ItemsTemplate, ItemsPanelTemplate).
Rich
+4  A: 

If you want your items to all have the same size, I would go for a UniformGrid. It one of those overlooked controls, might be very useful in this situation.

This is how I made a quick-and-dirty toolbar:

<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>
Pieter Breed
Cool. That worked. However, I already changed to two listview since it was rather urgent. Nevertheless, I will remember the uniform grid as my tests were rather conclusives.
David Brunelle