我有以下 XAML:-
<Grid Width="400" Height="400">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Heading" />
<ListBox Grid.Row="1" ItemsSource="{Binding Foo}"
Margin="0,12,0,12" />
<Button Grid.Row="2" Content="Button"
VerticalAlignment="Top" HorizontalAlignment="Left" />
</Grid>
这只是在列表框下方显示一个标题、一个列表框和一个按钮。随着列表框中项目数量的增加,按钮被按下,但是列表框将继续增长并最终从窗口底部消失,并带走按钮。
相反,我希望列表框增长,直到按钮到达窗口底部。此时列表框不应进一步增长,而是显示滚动条来滚动列表。我错过了什么?
编辑:
我刚刚想出了以下内容,这似乎可以解决问题。不确定是否有更优雅的解决方案?
<Grid Width="400" Height="400">
<Grid.RowDefinitions >
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Heading" />
<DockPanel Grid.Row="1" LastChildFill="True" VerticalAlignment="Top">
<Button DockPanel.Dock="Bottom" Content="Button"
VerticalAlignment="Top" HorizontalAlignment="Left" />
<ScrollViewer VerticalScrollBarVisibility="Auto" Margin="0,12,0,12">
<ListBox ItemsSource="{Binding Foo}" />
</ScrollViewer>
</DockPanel>
</Grid>