WPF:如何使空的TextBlock不占用空间? [英] WPF: How to make empty TextBlock not to occupy space?
问题描述
假设我有一个简单的布局,例如:
Let's say that I have a simple layout such as this:
<StackPanel>
<TextBlock Text="{Binding Path=Title}" />
<TextBlock Text="{Binding Path=ShortDescription}" />
<TextBlock Text="{Binding Path=LongDescription}" />
</StackPanel>
现在,当我将ShortDescription设置为null或空字符串时,第二个TextBlock仍然存在间隙.是否有一些属性可以防止空的文本块占用空间?还是应该使用其他控件?
Now when I have ShortDescription set to null or empty string there's still a gap in place of second TextBlock. Is there some property to prevent an empty textblock from occupying space? Or should I use some other control?
谢谢.
推荐答案
您要将文本框的可见性设置为"Collapsed".
You want to set the visibility of the textbox to "Collapsed".
可见性可以是:
可见-自我解释
隐藏-不可见,但仍会占用空间
收合-看不见且不占用空间
Visibility can be either:
Visible - Self explanatory
Hidden - Invisible but still takes up space
Collapsed - Invisible and takes up no space
您可能应该设置一个触发器,如下所示:
You should probably set up a trigger, like so:
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
这篇关于WPF:如何使空的TextBlock不占用空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!