WPF: ListBox with WrapPanel, vertical scrolling problem WPF: ListBox with WrapPanel, vertical scrolling problem wpf wpf

WPF: ListBox with WrapPanel, vertical scrolling problem


I think you better go with override the ItemPanelTemplate:

<Grid><ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">    <ListBox.ItemsPanel>        <ItemsPanelTemplate>            <WrapPanel IsItemsHost="True" />        </ItemsPanelTemplate>    </ListBox.ItemsPanel>    <ListBoxItem>listbox item 1</ListBoxItem>    <ListBoxItem>listbox item 2</ListBoxItem>    <ListBoxItem>listbox item 3</ListBoxItem>    <ListBoxItem>listbox item 4</ListBoxItem>    <ListBoxItem>listbox item 5</ListBoxItem></ListBox>


Well, I finally stumbled upon the solution. I was adding my UserControl to a placeholder panel that looked like this:

            <ScrollViewer Margin="20" >                <StackPanel Name="contentPanel"></StackPanel>            </ScrollViewer>

However, when I switched it to a Grid instead, things started to work the way I wanted:

<Grid Name="contentPanel" Margin="20" />

I think it has to do with the StackPanel not taking up all the vertical space by default, like the Grid is doing.


All I had to do was set the following, and the problem went away:

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">