ListBox in Grid Row with Auto height. Scrollbar is

2019-07-04 05:35发布

问题:

I have the following grid:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Border Height="50" Background="Gainsboro" Grid.Row="0"/>
    <Border Background="AliceBlue" Grid.Row="1">
        <ListBox ScrollViewer.VerticalScrollBarVisibility="Auto" ItemsSource="asdasdfasdf3dfasdf"/>
    </Border>
    <Border Height="60" Background="Aquamarine" Grid.Row="3"/>
</Grid>

Why ListBox's scrollviewer is not enabled? Last border is pushed out of a window. If i set the Grid.Row 2 Height to * star - it works nicely. Is it possible to have listbox with scrollviewer in auto height grid row?

回答1:

Auto makes your control resize as it needs. Therefore your ListBox will resize itself to show all its content and the scroll bar will never been shown.

* makes your control to take the available size, and it does not grow beyond that.

What you need to do is either define the MaxHeight property for the ListBox or use * as height in RowDefinition.