更改 StackPanel 中的可见性

发布于 2024-10-20 12:12:20 字数 823 浏览 3 评论 0原文

我有一个 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>

请注意,文本框以“隐藏”开头。

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

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

目的是隐藏标签,并使文本框出现在其位置。

但是,由于它是 StackPanel,因此 TextBox 会占用垂直空间,即使它不可见也是如此。然后,当文本框显示时,它上面有空白区域,标签以前是可见的。

有没有一种好方法可以使这两个项目本质上直接位于彼此之上?这样双击标签似乎突然变成了文本框?

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

<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".

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.

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫九 2024-10-27 12:12:20

请改用Visibilty.Collapsed。它不像 Visibilty.Hidden 那样保留空白。

Use Visibilty.Collapsed instead. It doesn't reserve the whitespace like Visibilty.Hidden does.

彩虹直至黑白 2024-10-27 12:12:20

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

You should try using Visibility.Collapsed instead of Visibility.Hidden.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文