wpf 当其他内容折叠时,网格内容会扩展以填充容器吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11885558/
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
Grid content expand to fill container when other content is collapsed?
提问by Pj.
I have a window with this basic layout:
我有一个具有这种基本布局的窗口:
<Window x:Class="GridStuffs.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Button Grid.Row="0" Click="TopButtonClick" VerticalAlignment="Stretch">Top</Button>
<Button Grid.Row="1" Name="_bottomButton">Bottom</Button>
</Grid>
Which is just displaying two buttons 'top' and 'bottom', each taking up equal vertical space in the window.
这只是显示两个按钮“顶部”和“底部”,每个按钮在窗口中占据相等的垂直空间。
Clicking the top button executes the following:
单击顶部按钮执行以下操作:
private void TopButtonClick(object sender, RoutedEventArgs e)
{
if (_bottomButton.Visibility == Visibility.Collapsed)
{
_bottomButton.Visibility = Visibility.Visible;
}
else
{
_bottomButton.Visibility = Visibility.Collapsed;
}
}
...which toggles the Visibity of the bottom button between Collapsed and Visible.
...在折叠和可见之间切换底部按钮的可见性。
What I want to happen is have the top button resize to fill the window when the bottom button is collapsed. What's actually happening is the bottom button is hidden, but the top button retains it's original size.
我想要发生的是在底部按钮折叠时调整顶部按钮的大小以填充窗口。实际发生的是底部按钮被隐藏,但顶部按钮保留其原始大小。
Question: What wpf/xaml magic do I have to do to have the top button expand to fill the window when the bottom button is collapsed?
问题:当底部按钮折叠时,我必须做什么 wpf/xaml 魔术才能让顶部按钮扩展以填充窗口?
回答by Rachel
Set the top RowDefinitionto Height="*", meaning it will take up all available space, and set the second RowDefinitionto Height="Auto"meaning it will take up as much space as it needs to show its content,
将顶部设置RowDefinition为Height="*",这意味着它将占用所有可用空间,并将第二个设置RowDefinition为Height="Auto"意味着它将占用显示其内容所需的空间,
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
In the event that your 2nd Button's height is not defined and should be 50% of the Grid's height, I would use a converter to bind the Button.Heightproperty to half of the parent Grid's height
如果您的第二个按钮的高度未定义并且应该是网格高度的 50%,我将使用转换器将该Button.Height属性绑定到父网格高度的一半
<Button Height="{Binding ActualHeight,
RelativeSource={RelativeSource Grid},
Converter={StaticResource HalfOfValueConverter}}" />
If you're interested, I actually have a MathConverterposted on my blogthat I use for any kind of mathematical equations with a bound value.
如果您有兴趣,我实际上在我的博客上MathConverter发布了一篇文章,我将其用于任何类型的具有边界值的数学方程。
<Button Height="{Binding ActualHeight,
RelativeSource={RelativeSource Grid},
Converter={StaticResource MathConverter},
ConverterParameter=@VALUE/2}" />

