Preventing items in ListBox from exceeding its width
c#, itemscontrol, listbox, listboxitem, wpf
Solution
This will disable the horizontal scrolling (even though it sounds like it's only disabling visibility):
<ListBox HorizontalContentAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
Problem
I'm sure this is simple, but I just can't seem to figure it out. Basically, I'm trying to force `ListBoxItem` from allowing itself to go outside of `ListBox` width. For example, let's say we have a crude `ListBox` with a `TextBox` for each `ListBoxItem`: ``` <ListBox HorizontalContentAlignment="Stretch"> <ListBoxItem> <TextBox TextWrapping="Wrap"/> </ListBoxItem> <ListBoxItem> <TextBox TextWrapping="Wrap"/> </ListBoxItem> <ListBoxItem> <TextBox TextWrapping="Wrap"/> </ListBoxItem> </ListBox> ``` If you were to type in them, the text, eventhough it has `TextWrapping` set to `Wrap`, will continue flowing to the right, as the width of the `ListBoxItem` and the `ListBox` adjust to the width of the content (note that the `HorizontalScrollBar` appears): I'm sure this is an intended behavior and is probably caused by the `ScrollViewer` within the template, but I want the text to wrap and be contained within the original width. I can solve this by setting a static width, but it would be a sketchy design decision and not something I want to do (it would restrict things from being re-sized easily). Basically, the behavior I'm seeking is that of an `ItemsControl`: ``` <ItemsControl> <TextBox TextWrapping="Wrap"/> <TextBox TextWrapping="Wrap"/> <TextBox TextWrapping="Wrap"/> </ItemsControl> ``` I just want to keep the `ListBox` control because of its other behaviors, which I need. Any ideas?