2010-03-26 83 views

回答

26

您可以将行内容的可见性设置为“折叠”。这仅在RowDefinition的Height属性设置为“Auto”时才起作用,因此行大小基于其内容。

例如,

<Grid> 
    <Grid.RowDefinitions> 
    <RowDefinition Height="Auto" /> 
    <RowDefinition Height="Auto" /> 
    <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 

    <Border Grid.Row="0" BorderThickness="1" BorderBrush="Red"><TextBlock>Visible Row</TextBlock></Border> 
    <Border Grid.Row="1" BorderThickness="1" BorderBrush="Black" Visibility="Collapsed"><TextBlock>Hidden Row</TextBlock></Border> 
    <Border Grid.Row="2" BorderThickness="1" BorderBrush="Red"><TextBlock>Visible Row</TextBlock></Border> 
</Grid> 
+0

正是我一直在寻找。谢谢。 – 2010-03-26 09:35:15

+1

如果您有多列,你会怎么做?隐藏每个元素都不太实际... – 2012-12-01 14:36:04

+1

Clement-将容器中的每个元素包装在容器中,例如另一个网格,并简单地显示/隐藏/折叠容器的可见性。 – Kurren 2013-04-16 09:08:42

3

其实我只是问了同样的问题,几天以前,这里看看:

Hide grid row in WPF

基本上设置rowHeight为自动,然后设置可见性=“折叠”将为您隐藏行。我遇到的唯一问题是边缘,但那是微不足道的。至少该行被隐藏了。

+1

关于利润的好处,谢谢。 – 2010-03-26 09:34:55

1

只是这样做:

XAML:

<Grid.RowDefinitions> 
    <RowDefinition Height="1*" x:Name="name1" /> 
    <RowDefinition Height="Auto" x:Name="name2" /> 
    <RowDefinition Height="Auto" /> 
    <RowDefinition Height="Auto" /> 
</Grid.RowDefinitions> 

C#的崩溃:

name1.Height = new GridLength(0); 
name2.Height = new GridLength(0); 

C#能见度:

name1.Height = new GridLength(1, GridUnitType.Star); 
name2.Height = GridLength.Auto;