WPF: get the content of a GroupBox to fill available space

Master_T picture Master_T · Jun 27, 2011 · Viewed 7.1k times · Source

I'm facing an irritating problem with WPF GroupBox, hope someone can help me out. Basically the problem is this: I have a listview inside a GroupBox, but no matter what I do I can't seem to be able to make it fill the GroupBox.

Here is the basic code:

<GroupBox Grid.Row="2" Header="Field" Visibility="{Binding ElementName=radioUnbound, Path=IsChecked, Converter={StaticResource bool2vis}}" Margin="0" VerticalContentAlignment="Stretch">
        <ListView ItemsSource="{Binding ElementName=nnf1, Path=UnboundFields}" x:Name="listUnbound" SelectionChanged="listSelectionChanged" VerticalAlignment="Stretch" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding name}" Margin="2"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </GroupBox>

I tried encasing the list inside Grids, StackPanels, DockPanel, etc... but no matter what I try I always invariably end up with this:

enter image description here

Answer

thumbmunkeys picture thumbmunkeys · Jun 27, 2011

I tried your code in XamlPad it works as you would expect it. Make sure you don't have global styles that set your ListView or GroupBox appearance.

You can clear global styles by putting this in the resources section of the GroupBox's parent control:

<Style TargetType="GroupBox" />
<Style TargetType="ListView" />