views:

177

answers:

1

Hello,

I am attempting to resize the contents of my listbox according to the listbox itself. This is being done in WPF.

Any ideas on how this might be possible?

I have been searching and have not come up with anything obtainable...

Any helpful ideas are appreciated,

Thanks!

A: 

I assume when you say "resize" you mean that you want to stretch the items in both directions. To take a default ListBox and stretch the items horizontally all you need is:

<ListBox HorizontalContentAlignment="Stretch"/>

The default is Left so all the ListBoxItems end up pushed to the left and sized individually based on their content.

Vertical stretching requires getting rid of the StackPanel used to do layout for the items because it has no concept of resizing its children in the direction of Orientation. The simplest thing to use is a UniformGrid but you might want something more custom depending on how you want the items to size relative to each other. You'll also need to do the same thing with the VerticalContentAlignment setting (Center by default). So here's one that will stretch items both ways:

<ListBox HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="1"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>
John Bowen