0
votes

I have a grid that hosts a stackpanel that hosts a listbox. The Listbox once filled from the itemssource is stretching out of the visible area of the stackpanel. I have tried limiting the grid and stackpanel in size and the listbox continues to stretch out of the visible range (it just goes to edge and continues as if nothing was there to stop it instead of limiting it's size and bringing up a vertical scrollbar). The only thing that seems to help is setting the MaxHeight on the listbox, the issue is I can't tell what that height should be for different clients. I have tried different VerticalAlignments and I have the bottom margin set to 5 to try and get it to stop at the edge but nothing has had an effect yet.

Snip: Edit to add full hierarchy:

<UserControl>
    <Grid x:Name="LayoutRoot" Background="White" Margin="0,0,0,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition MinWidth="230"/>
            <ColumnDefinition d:DesignWidth="500"/>
            <ColumnDefinition MinWidth="300" />
        </Grid.ColumnDefinitions>
    <StackPanel x:Name="ContentHolder" Grid.Column="0" DataContext="{Binding}" VerticalAlignment="Top">
                <ListBox Name="lst" ItemsSource="{Binding}" Margin="5,0,15,5" VerticalAlignment="Stretch">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Name}" />
                                <TextBlock Text="{Binding Number}" Margin="15,0,0,0" />
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </StackPanel>
    </grid>
</usercontrol>

I have removed most of the names and such from the code to just use this as an example.

1
Can you post the XAML around your StackPanel? StackPanel's have an infinite size for their children unless constrained by their parent. - Stephan
Done. I didn't know that this was the case with the infinite size. Is that true even if the Stackpanel itself is set to a certain maximum? - Mark
Ok you are right the answer was to put everything in a grid and move out of the stackpanel. Sort of annoying that the stack panel was providing no limit based on the size of the area it was contained in. Thank you for the answer. I'd check you if I could :-) - Mark

1 Answers

0
votes

Answering this for anyone confused and looking for an answer in the future. Basically stackpanels step to a vertical orientation do not limit the size of their child controls vertically so anything with a scroll bar (datagrid, listbox, etc) probably shouldn't be used with them.