如何隐藏空的 TextBlock? [英] How to hide the empty TextBlock?
本文介绍了如何隐藏空的 TextBlock?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面提供的 XAML 中,有时我没有电话的值.发生这种情况时,该值丢失,但 TextBlock
仍在面板中占据空间.我想隐藏空 TextBlock
以防止在 StackPanel
中占用空间.
In the XAML provided below, I don't have the value for Phone sometimes. When that happens, the value is missing, but the TextBlock
is still occupying the space in the panel. I want to hide empty TextBlock
s from taking space in the StackPanel
.
这是 XAML:
<StackPanel>
<TextBlock Text="{Binding Path=FirstName}" />
<TextBlock Text="{Binding Path=LastName}" />
<TextBlock Text="{Binding Path=Phone}" />
<TextBlock Text="{Binding Path=Email}" />
</StackPanel>
我已阅读这篇文章,但接受的答案对我不起作用:
I've read this article, but the accepted answer doesn't work for me:
<StackPanel>
<TextBlock Text="{Binding Path=FirstName}" />
<TextBlock Text="{Binding Path=LastName}" />
<TextBlock Text="{Binding Path=Phone}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<TextBlock Text="{Binding Path=Email}" />
</StackPanel>
我是在某处犯了错误,还是接受的答案是错误的?我应该怎么做才能实现我的目标?
Am I making a mistake somewhere, or is the accepted answer wrong? What should I do to achieve my goal?
推荐答案
您可能需要使用:
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="Text" Value="">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
</Style.Triggers>
</Style>
或者两者兼而有之:
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="Text" Value="">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
</Style.Triggers>
</Style>
这篇关于如何隐藏空的 TextBlock?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文