为什么ScrollViewer会覆盖wpf中的网格行高?

时间:2016-07-26 09:47:50

标签: wpf

我有一个ScrollViewer,其中包含Grid,其中包含两行高度'*'

<ScrollViewer>
    <Grid HorizontalAlignment="Stretch">
      <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
      </Grid.RowDefinitions>
      <GroupBox Grid.Row="0" Grid.Column="0"                    
                  Header="XYZ" 
                  VerticalAlignment="Stretch"    
                  HorizontalAlignment="Stretch"
                  Margin="0,10,0,0" 
                  Width="Auto" MinWidth="160"
                  BorderThickness="0"
                  Style="{StaticResource MyGroupBoxStyle}">
        <ListBox Name="lstMentorGroups" IsSynchronizedWithCurrentItem="True"                      
                  ItemsSource="{Binding  Path=MyCollection}"
                  HorizontalAlignment="Stretch"
                  VerticalAlignment="Stretch"
                  Style="{StaticResource MyListBoxStyle}">
          <ListBox.ItemTemplate>
            <DataTemplate >
              <TextBlock Text="{Binding Path=Name}"/>
            </DataTemplate>
          </ListBox.ItemTemplate>
        </ListBox>
      </GroupBox>

      <GroupBox Grid.Row="1" Grid.Column="0" MinWidth="160"          
                  Header="ABC" Margin="0,10,0,0"
                  VerticalAlignment="Stretch" 
                  HorizontalAlignment="Stretch"
                  BorderThickness="0"
                  Style="{StaticResource MyGroupBoxStyle}">
        <ListBox ItemsSource="{Binding  Path=List1, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                  Style="{StaticResource MyListBoxStyle}" Margin="0,0,5,0">
          <ListBox.ItemTemplate>
            <DataTemplate >
              <TextBlock Text="{Binding Path=Prop1}"/>
            </DataTemplate>
          </ListBox.ItemTemplate>
        </ListBox>
      </GroupBox>
    </Grid>
</ScrollViewer>

但是在运行时,第一组框会获得其包含列表框所需的全高,这意味着它会将网格高度*覆盖为自动

如果我不使用scrollviewer它可以正常工作,它可以为每个组合框提供50-50%的高度。

3 个答案:

答案 0 :(得分:0)

如果它位于ScrollViewer中,您需要设置网格的高度,否则网格将具有所需的高度,这意味着网格中的ListBox将获得无限高度以显示其项目。

答案 1 :(得分:0)

要确保两行的高度相同,您应该使用SharedSizeGroupe 所以你应该做的是:

<ScrollViewer>
<Grid HorizontalAlignment="Stretch" Grid.IsSharedSizeScope="True">
  <Grid.RowDefinitions>
    <RowDefinition SharedSizeGroup="A"  />
    <RowDefinition SharedSizeGroup="A"  />
  </Grid.RowDefinitions>
  <GroupBox Grid.Row="0" Grid.Column="0"                    
              Header="XYZ" 
              VerticalAlignment="Stretch"    
              HorizontalAlignment="Stretch"
              Margin="0,10,0,0" 
              Width="Auto" MinWidth="160"
              BorderThickness="0"
              Style="{StaticResource MyGroupBoxStyle}">
    <ListBox Name="lstMentorGroups" IsSynchronizedWithCurrentItem="True"                      
              ItemsSource="{Binding  Path=MyCollection}"
              HorizontalAlignment="Stretch"
              VerticalAlignment="Stretch"
              Style="{StaticResource MyListBoxStyle}">
      <ListBox.ItemTemplate>
        <DataTemplate >
          <TextBlock Text="{Binding Path=Name}"/>
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
  </GroupBox>

  <GroupBox Grid.Row="1" Grid.Column="0" MinWidth="160"          
              Header="ABC" Margin="0,10,0,0"
              VerticalAlignment="Stretch" 
              HorizontalAlignment="Stretch"
              BorderThickness="0"
              Style="{StaticResource MyGroupBoxStyle}">
    <ListBox ItemsSource="{Binding  Path=List1, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
              HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
              Style="{StaticResource MyListBoxStyle}" Margin="0,0,5,0">
      <ListBox.ItemTemplate>
        <DataTemplate >
          <TextBlock Text="{Binding Path=Prop1}"/>
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
  </GroupBox>
</Grid>

答案 2 :(得分:0)

由于Grid放置在ScrollViewe r中,因此需要整个空间控制所需的数量。而且我不想显示垂直滚动条。

我只是将Grid MaxHeight设置为ScrollViewer ActualHeight

MaxHeight="{Binding ElementName=ScrollViewer, Path=ActualHeight}"