使ScrollViewer填充动态区域

bai*_*alk 4 wpf xaml scrollviewer

在下面的示例中,我无法让ScrollViewer填充可用空间,由于上面的动态内容,高度未知但是它是否无法填充可用空间而不是过度运行?

   <Grid x:Name="Main" Height="200" MaxHeight="200">
      <StackPanel>
         <Grid x:Name="MainContent" Height="170" MaxHeight="170">
            <StackPanel>
               <TextBlock FontSize="24" Text="Dynamic data "/>
               <TextBlock FontSize="24" Text="height "/>
               <TextBlock FontSize="24" Text="unknown... "/>
               <Grid x:Name="Results" Background="Red">
                  <ScrollViewer>
                     <StackPanel>
                        <TextBlock FontSize="24" Text="Result set... 0"/>
                        <TextBlock FontSize="24" Text="Result set... 1"/>
                        <TextBlock FontSize="24" Text="Result set... 2"/>
                        <TextBlock FontSize="24" Text="Result set... 3"/>
                     </StackPanel>
                  </ScrollViewer>
               </Grid>
            </StackPanel>
         </Grid>
         <Grid x:Name="Nav">
            <Button HorizontalAlignment="Left" Content="Back"/>
            <Button HorizontalAlignment="Right" Content="Forward"/>
         </Grid>
      </StackPanel>
   </Grid>
Run Code Online (Sandbox Code Playgroud)

dko*_*ozl 5

MainContent Grid使用DockPanel而不是StackPanelLastChildFill=True,像这样:

<Grid x:Name="MainContent" Height="170" MaxHeight="170">
    <DockPanel LastChildFill="True">
        <TextBlock DockPanel.Dock="Top" FontSize="24" Text="Dynamic data "/>
        <TextBlock DockPanel.Dock="Top" FontSize="24" Text="height "/>
        <TextBlock DockPanel.Dock="Top" FontSize="24" Text="unknown... "/>
        <Grid x:Name="Results" Background="Red">
            <ScrollViewer>
                <StackPanel>
                    <TextBlock FontSize="24" Text="Result set... 0"/>
                    <TextBlock FontSize="24" Text="Result set... 1"/>
                    <TextBlock FontSize="24" Text="Result set... 2"/>
                    <TextBlock FontSize="24" Text="Result set... 3"/>
                </StackPanel>
            </ScrollViewer>
        </Grid>               
    </DockPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)

然后最后一个元素DockPanel将自己调整到可用空间