Wpf – Make a ListBox Fill the Width of the Parent Container

listboxwpfwpf-controls

I have a DockPanel with a listbox in it. The dockpanel successfully fills the width of the entire window. Now, i want the Listbox to fill the width of the entire DockPanel.

I have tried many things… including wrapping a grid around it and setting the column to width of *, setting the horizontalalignment of the listbox to stretch, and numerous others. I cannot seem to get this silly listbox to fill the width.

This listbox DOES have an ItemTemplate, but everything inside it has a horizontal alignment of stretch with auto width. Whenever you set the ItemTemplate's width to something static, the listbox does resize appropriately. I just cannot get it to fill the parent.

Thanks.

EDIT:

Thanks guys for making me realize it's my fault… Turns out it was a goof in the style Tag.

I will attempt to remove this question now.

Best Answer

I would bet that your ListBox is filling the width of its parent container, but the ListBoxItems within your list box are not stretching horizontally. Try adding the following style:

 <ListBox.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
         <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter> 
     </Style> 
 </ListBox.ItemContainerStyle> 
Related Topic