Yesterday I was working on a Silverlight 3 beta application which didn't have fixed widths or heights, as I wanted the whole layout to expand and contract as the browser window did. For the layout I used StackPanels, as they seemed to be the most intuitive layout element due to their simple function - laying out items either horizontally or vertically.

Innocently, I created a ListBox that contained a TextBlock that was set to wrap. Unfortunately, the ListBox didn't respect its parent StackPanel container and the text extended off the right side of the page. My short-term hack was to hard-code in a width for the TextBlock but that meant that my ListBox didn't expand and contract as the page did.

I then created a large number of items in the ListBox and the bottom items disappeared off the bottom of the page. I knew that the ListBox internally contained a ScrollViewer so I was surprised why this wasn't invoked when it went off the page, again seeing the ListBox having no respect for its parent StackPanel container.

The solution was to replace the StackPanels that were parents of the ListBox with Grids and to set a property on the ListBox:

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">

This forced the ListBox to stay within the boundaries of the Grid and triggered the ListBox's internal vertical scrollbar to be invoked when it reached the bottom of the page. You can see an example of the listbox behaving itself here.