4

I have an Expander that I want to have a ListBox in. When the I open the Expander, the ListBox just expands off the screen (rather than expanding to fill what is available and then scrolling).

Here is my XAML:

<DockPanel Margin="266.25,0,455,12" Name="dockPanel1">
    <StackPanel>
        <Expander Header="expander1" Name="expander1" Width="150" HorizontalAlignment="Left">
            <Grid>
                <Label>Testing</Label>
                <ScrollViewer>
                    <ListBox Name="lstBox"  FontSize="14" SelectionChanged="lstBox_SelectionChanged" />
                </ScrollViewer>
            </Grid>
        </Expander>
        <Expander Header="expander2" Name="expander2" Width="150" HorizontalAlignment="Left">
            <Grid >

            </Grid>
        </Expander>
    </StackPanel>
</DockPanel>

When Expander1 is opened then it just expands to the size of the ListBox (off the screen). If I put a size on the grid (Height="275"), then it does not resize with the window.

I want it to stretch to the size of the window, but no more. Is there a way to do that?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Vaccano
  • 78,325
  • 149
  • 468
  • 850

1 Answers1

2

You need to set the Height property of ScrollViewer otherwise it will be the same size as it's child. Here is some updated XAML:

<DockPanel>
    <StackPanel>
        <Expander Header="expander1" Width="150" HorizontalAlignment="Left">
            <StackPanel>
                <Label>Testing</Label>
                <ScrollViewer Height="75">
                    <ListBox>

                    </ListBox>
                </ScrollViewer>
            </StackPanel>
        </Expander>
        <Expander Header="expander2">
        </Expander>
    </StackPanel>
</DockPanel>
Jake Pearson
  • 27,069
  • 12
  • 75
  • 95
  • But how can I set that to be the height of the window? Can I do data binding to make that happen? – Vaccano Nov 02 '09 at 15:57
  • You could do a binding with syntax like this: {Binding ElementName=Window1, Path=Height} but that would lead to too large of a value. I think instead you will need to a property to your window that is Height - SomeOffset. – Jake Pearson Nov 02 '09 at 16:06
  • 1
    Also, I imagine there is a better solution than what I just suggested. – Jake Pearson Nov 02 '09 at 16:15