更改 StackPanel 中的可见性
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请改用
Visibilty.Collapsed
。它不像 Visibilty.Hidden 那样保留空白。Use
Visibilty.Collapsed
instead. It doesn't reserve the whitespace likeVisibilty.Hidden
does.您应该尝试使用
Visibility.Collapsed
而不是Visibility.Hidden
。You should try using
Visibility.Collapsed
instead ofVisibility.Hidden
.