Search code examples
silverlightlistboxwrappanel

Using a WrapPanel in a ListBox in Silverlight


I have a Silverlight application that has a ListBox of a specific width. I am dynamically adding items to this ListBox in my code-behind. If the items require more space than is allotted, I would like the items to wrap to the next line AND the height of the ListBox to increase. As it runs now, a horizontal scroll bar appears in my ListBox. In addition, no wrapping, thus no growing happens. What am I doing wrong? Here is my ListBox:

<ListBox x:Name="myListBox" Grid.Column="1" Width="600" MinHeight="24">
  <ListBox.ItemsPanel>
    <ItemsPanelTemplate>
      <controlsToolkit:WrapPanel />
    </ItemsPanelTemplate>
  </ListBox.ItemsPanel>
</ListBox>

Thank you for your help!


Solution

  • Try setting ScrollViewer.HorizontalScrollBarVisibility to Disabled on the ListBox to prevent the horizontal scrolling and force the wraping.

    <ListBox x:Name="myListBox" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Grid.Column="1" Width="600" MinHeight="24">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <controlsToolkit:WrapPanel />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>