tags:

views:

3174

answers:

2

Hello,

I want to lay out items in a ListView in a similar manner to the WinForms ListView in List mode. That is, where items are laid out not just vertically but horizontally in the ListView as well.

I don't mind if the items are laid out like this:

1 4 7
2 5 8
3 6 9

Or like this:

1 2 3
4 5 6
7 8 9

As long as they are presented both vertically and horizontally in order to maximize the use of available space.

The closest I could find was this question:

http://stackoverflow.com/questions/359217/how-do-i-make-wpf-listview-items-repeat-horizontally-like-a-horizontal-scrollbar

Which only lays out the items only horizontally.

+9  A: 
rmoore
Thanks, I had tried using a WrapPanel previously, as suggested by Dennis, but the Width binding is needed which the example I was working from didn't have.
Groky
Apparently the only important thing is the Width. The others are optional.
Tigraine
+3  A: 

I recently research how to achieve this in WPF and found a good solution. What I wanted was to the replicate the List mode in Windows Explorer, i.e. top-to-bottom, then left-to-right.

Basically what you want to do override the ListBox.ItemsPanel property to use a WrapPanel with it's orientation set to Vertical.

<ListBox>
  <ListBox.ItemsPanel>
    <ItemsPanelTemplate>      
      <WrapPanel Orientation="Vertical"/>
    </ItemsPanelTemplate>
  </ListBox.ItemsPanel>
</ListBox>

However this WILL be slow when loading a large data set as it the wrap panel is not virtualised. This is important. So this task now becomes a little more as now you need to write your own VirtualizedWrapPanel by extending VirtualizedPanel and implementing IScrollInfo.

public class VirtualizedWrapPanel : VirtualizedPanel, IScrollInfo
{
   // ...
}

This is as far as I got in my research before having to go off to another task. If you want more information or examples, please comment.

EDIT: Ben Constable's has a great series on how to implement IScrollInfo. http://blogs.msdn.com/bencon/archive/2006/01/05/509991.aspx

There are 4 articles in total. A really good read.

HTH,

Dennis

Dennis Roche
shouldn't the source be ItemsPanelTemplate wrapped in ListBox.ItemsPanel and not the other way around? Your version does not compile.
Sam
@Sam: You are correct. I must have written that code directly into StackOverflow (i.e. did not check it). Thank-you for picking that up.
Dennis Roche