XAML布局问题

时间:2013-06-24 11:05:07

标签: xaml

我有以下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>

1 个答案:

答案 0 :(得分:0)

您需要将包含ListBox的网格行的高度限制为某个固定高度,以阻止它占据所需的垂直高度。我要远离IDE进行测试,但您可能还需要将VerticalScrollBarVisibility上的ListBox设置为自动。

<Grid Width="400" Height="400">  
   <Grid.RowDefinitions>
     <RowDefinition Height="auto"/>
     <RowDefinition Height="200"/>
     <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>