wpf 更改 StackPanel 中的可见性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5224918/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:38:22  来源:igfitidea点击:

Changing Visibility in a StackPanel

wpfvisibilitystackpanel

提问by abelenky

I have a WPF StackPanel that looks like this: (some attributes removed that don't matter)

我有一个看起来像这样的 WPF StackPanel :(删除了一些无关紧要的属性)

<StackPanel HorizontalAlignment="Center" Name="PICStack">
        <Label Name="PICName"  MouseDoubleClick="PICName_MouseDoubleClick" />
        <TextBox Name="PICData" Width="120" Visibility="Hidden" />
        <Label Name="PICWeight" />
        <Label Name="PICARM"    />
</StackPanel>

Note that the TextBox starts as "Hidden".

请注意,TextBox 以“隐藏”开头。

When I double-click on the top label, I swap the visibility:

当我双击顶部标签时,我交换了可见性:

private void PICName_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    this.PICData.Visibility = Visibility.Visible;
    this.PICName.Visibility = Visibility.Hidden;
}

The intent is to hide the label, and make the TextBox appear in its place.

目的是隐藏标签,并使 TextBox 出现在它的位置。

However, because it is a StackPanel, the TextBox takes up vertical space, even when it is not visible. Then, when the TextBox is revealed, it has blank space above it, where the Label was previously visible.

然而,因为它是一个 StackPanel,TextBox 占用垂直空间,即使它不可见。然后,当 TextBox 被显示时,它上面有空白,标签以前是可见的。

Is there a good way to make the two items essentially be directly on top of each other? so that double-clicking the Label appears to suddenly change into a TextBox?

有没有一种好方法可以使这两个项目基本上直接重叠?以至于双击 Label 似乎突然变成了 TextBox?

回答by Dean Kuga

Use Visibilty.Collapsedinstead. It doesn't reserve the whitespace like Visibilty.Hiddendoes.

使用Visibilty.Collapsed来代替。它不像那样保留空格Visibilty.Hidden

回答by madd0

You should try using Visibility.Collapsedinstead of Visibility.Hidden.

您应该尝试使用Visibility.Collapsed而不是Visibility.Hidden.