5

在下面的示例中,我无法让 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>
4

3 回答 3

8

in MainContent GriduseDockPanel而不是StackPanelwith LastChildFill=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>

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

于 2013-06-04T14:35:51.080 回答
0

我通过为 Scrollviewer 的高度添加另一个元素来解决它:

<grid Grid.row="3" x:Name="Mirror">
</grid>
<ScrollViewer Grid.row="3" Height="{Binding ElementName=uxMirror,XPath=ActualHeight}">
</ScrollViewer>
于 2016-05-17T02:55:07.850 回答
-1

Firstly your layout is kind of a mess. Why do you need to put a StackPanel as the only child of a Grid. For the Rows? Use RowDefinitions and ColumnDefinitions when dealing with Grid

From my understanding of your layout, you can pretty much do everything you need with just a Grid and a DockPanel. When dealing with layout containers, Aim to get your layout with the "least" number of containers as "efficiently" possible. Even if it aint any real performance benefit, it helps enormously when going through someone else's code to not have redundant nesting

<Grid x:Name="Main">
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="*" />
    <RowDefinition Height="Auto" />
  </Grid.RowDefinitions>
  <TextBlock Grid.Row="0"
              FontSize="24"
              Text="Dynamic data " />
  <TextBlock Grid.Row="1"
              FontSize="24"
              Text="height " />
  <TextBlock Grid.Row="2"
              FontSize="24"
              Text="unknown... " />
  <ScrollViewer Grid.Row="3"
                Background="Red">
    <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>
  <DockPanel x:Name="Nav"
              Grid.Row="4"
              LastChildFill="False">
    <Button Content="Back"
            DockPanel.Dock="Left" />
    <Button Content="Forward"
            DockPanel.Dock="Right" />
  </DockPanel>
</Grid>

This should give you everything your looking for. If you need to restrict Height dimensions with static values, Add them in accordingly when absolutely needed.

As for the "Nav" DockPanel, yes you can using HorizontalAlignment get your Button's positioned Left and Right and thereby not use the DockPanel, but that would go against the concept of trying to keep "one" item in a Grid cell and hence the DockPanel usage.

于 2013-06-04T15:17:46.817 回答