Peanut
Peanut

Reputation: 2136

Make a ListBox Fill the Width of the Parent Container

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.

Upvotes: 4

Views: 8214

Answers (4)

David Ehnis
David Ehnis

Reputation: 120

I had to remove all 'alignment' properties from the listbox to get this to work. Be sure that the HorizontalAlignment and VerticalAlignment assignments are removed from the listbox definition. Once I did this AND wrapped it in a DockPanel, as suggested above, I was able to get it to work.

Upvotes: 1

Rover
Rover

Reputation: 2230

You can also use DockPanel.LastChildFill property:

http://msdn.microsoft.com/en-us/library/system.windows.controls.dockpanel.lastchildfill.aspx

Upvotes: 1

ColinE
ColinE

Reputation: 70142

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> 

Upvotes: 5

James Hay
James Hay

Reputation: 12700

A ListBox should fill the DockPanel by defualt.

I just tried:

<DockPanel>
  <ListBox Background="Red" />
</DockPanel>

and it worked perfectly. Will be hard for us to see what the problem is without a snippet of your code.

Upvotes: 4

Related Questions