The code below is the complete representation of the problem. Essentially, when I have a ListBox inside a ScrollViewer the ListBox won't show its own scrollbar. Rather, it relies on the scrollbar of the parent ScrollViewer. In my situation I have my entire control wrapped in a ScrollViewer so that I get scrollbars when the MinHeight/MinWidth are violated. The ListBox is just one of numerous controls in my UserControl and I don't want my UserControl jumping to wide proportions to match the items in the ListBox. What would you add to the code below to force the ListBox to use its own scrollbar?
using System;
using System.Windows;
using System.Windows.Controls;
namespace TestExpanderWidth
{
class Program
{
[STAThread]
static void Main()
{
var listbox = new ListBox { Margin = new Thickness(10.0), MinWidth = 400 };
listbox.Items.Add(new string('c', 3000));
var sv = new ScrollViewer {HorizontalScrollBarVisibility = ScrollBarVisibility.Auto, VerticalScrollBarVisibility = ScrollBarVisibility.Auto};
sv.Content = listbox; // remove for test
var window = new Window { Width = 600, Height = 400 };
window.Content = sv; // remove for test
//window.Content = listbox; // add for test
var application = new Application();
application.Run(window);
}
}
}