Visibility.Collapsed没有回馈它的空间

时间:2011-11-02 14:40:52

标签: silverlight xaml silverlight-4.0

奇怪的主题让我解释一下:我有一个Silverlight MainPage,页面顶部有一个边框控件400.一旦用户经过身份验证,我就会导航到一个子页面。我也折叠了MainPage上的边框。子页面显示正确但在其上方有一个空白的400空间,MainPage边框控件在折叠之前。我认为Visibilty.Collapsed应该放弃它占据的空间,但它似乎没有做到这一点。我在MainPage中的框架对象上设置了VerticalAlignment ='Top',并在子页面上尝试了该设置,但都没有工作。

有人有什么建议吗?感谢

代码已更新:

 <ScrollViewer>
    <Grid x:Name="LayoutRoot"
          DataContext='{StaticResource ViewModel}'>
        <Grid.RowDefinitions>
            <RowDefinition Height='400'></RowDefinition>
            <RowDefinition Height='*'></RowDefinition>
        </Grid.RowDefinitions>

1 个答案:

答案 0 :(得分:1)

您应该能够通过将边框包裹在网格中,然后将包含边框的RowDefinition的高度设置为Auto来解决此问题。以下测试示例对我有用:

<Grid x:Name="MainGrid" Height="760" VerticalAlignment="Top" Background="Azure">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition x:Name="HeaderRow"  Height="Auto"></RowDefinition>
        <RowDefinition x:Name="TabRow" Height="*"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>

    <Border BorderThickness="1" Grid.Row="0" Background="Bisque" 
                        HorizontalAlignment="Stretch"
                        VerticalAlignment="Stretch"
                        Name="HeaderBorder"
                        CornerRadius="10">
        <TextBlock>Test</TextBlock>
    </Border>
    <TextBlock Grid.Row="1">Test2</TextBlock>
    <Button Grid.Row="2" Content="Hide Border" Click="Button_Click"></Button>

</Grid>

在后面的代码中:

private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
    HeaderBorder.Visibility = Visibility.Collapsed;
}