如何在 WPF 网格中隐藏一行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2521854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I hide a row in a WPF grid?
提问by Montgomery 'monty' Jones
I have been hiding a row in a WPF grid by setting the Height
property to 0.
我通过将Height
属性设置为 0在 WPF 网格中隐藏了一行。
I was expecting something akin to a Visible
property.
我期待着类似于Visible
财产的东西。
Is there a more appropriate way to hide the row?
有没有更合适的方法来隐藏行?
回答by IanR
You could set the visibility of the row's content to "Collapsed". This will only work if the Height property of the RowDefinition is set to "Auto" so the row sizes based on it's content.
您可以将行内容的可见性设置为“已折叠”。这仅在 RowDefinition 的 Height 属性设置为“自动”时才有效,因此行大小基于其内容。
For example,
例如,
<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>
回答by Richard
I actually just asked the same question a couple of days ago, take a look here:
实际上我前几天刚问过同样的问题,请看这里:
Basically setting the RowHeight to Auto and then Setting the Visibility="Collapsed" will hide the row for you. The only issue I had was the Margins, but that was minor. At least the row got hidden.
基本上将 RowHeight 设置为 Auto 然后设置 Visibility="Collapsed" 将为您隐藏该行。我唯一的问题是边距,但那是次要的。至少这一行被隐藏了。
回答by Stephane G.
Just do this :
只需这样做:
XAML :
XAML:
<Grid.RowDefinitions>
<RowDefinition Height="1*" x:Name="name1" />
<RowDefinition Height="Auto" x:Name="name2" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
C# for collapse :
C# 崩溃:
name1.Height = new GridLength(0);
name2.Height = new GridLength(0);
C# for visibility:
C# 可见性:
name1.Height = new GridLength(1, GridUnitType.Star);
name2.Height = GridLength.Auto;