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 the ItemsPanelTemplate
or do I need to extend the ListBox
class itself? In either case, how would I got about it?
Thanks for your help!
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>
来源:https://stackoverflow.com/questions/1545519/change-how-items-are-displayed-wpf-list-box