tags:

views:

285

answers:

1

I have a WPF window which displays a ListBox. I've changed the ListBox's item template so that the list box displays rectangular items which can be selected.

Now I'd like to modify the ListBox's ItemsPanelTemplate so that the items are displayed in a grid instead of either a vertical or horizontal list. Specifically I'd like the first item to go in the top right corner of the ListBox and then second item below it and third under that until the height of the next item would make the column of items taller than the height of the ListBox itself. At that point it should start a second column directly to the right of the first item added. If the total width of all of the columns combined was greater than width of the ListBox then a horizontal scroll bar should appear.

Is there a way to get that effect just by modifying ItemsPanelTemplate or do I need to extend the ListBox class itself? In either case, how would I got about it?

Thanks for your help!

+3  A: 

I haven't tested this, but I suspect you can get the desired effect by swapping out the default panel used by the ListBox to a WrapPanel:

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

HTH, Kent

Kent Boogaart
Thanks for the help! That basically solves it, but the code above 'as is' still just creates a long vertical list.The trick to get it to create a grid as I described above is to add this attribute to the ListBox tag:ScrollViewer.VerticalScrollBarVisibility="Disabled"
Evan