stackpanel 中的 wpf 文本块自动调整大小/布局
我有以下堆栈面板
<StackPanel>
<TextBlock Text="{Binding AddressLine1}" />
<TextBlock Text="{Binding AddressLine2}" />
<TextBlock Text="{Binding AddressLine3}" />
<TextBlock Text="{Binding AddressLine4}" />
</StackPanel>
,并且绑定对象上的 AddressLine2 字符串为空。
我的堆栈面板呈现为类似的效果
| AddressLine1 |
| |
| AddressLine3 |
| AddressLine4 |
,但我希望它呈现为类似的效果,
| AddressLine1 |
| AddressLine3 |
| AddressLine4 |
这可能吗,还是我缺少一种明显的方法来做到这一点?
谢谢, 克里斯
I have the following stack panel
<StackPanel>
<TextBlock Text="{Binding AddressLine1}" />
<TextBlock Text="{Binding AddressLine2}" />
<TextBlock Text="{Binding AddressLine3}" />
<TextBlock Text="{Binding AddressLine4}" />
</StackPanel>
and my AddressLine2 string is null on the bound object.
My stack panel renders like
| AddressLine1 |
| |
| AddressLine3 |
| AddressLine4 |
but I want it to render like
| AddressLine1 |
| AddressLine3 |
| AddressLine4 |
is this possible, or am I missing an obvious way to do it?
Thanks,
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建一个实现 IMultiValueConverter 的转换器,然后在文本上使用 MultiBinding,这样每一行只有一个 TextBlock,如下所示:
在 XAML 中...
Create a converter that implements IMultiValueConverter then use a MultiBinding on the text so that you only have one TextBlock with each line like this:
And in the XAML...
您可以使用 ValueConverter 将 TextBlock 的 Visibility 绑定到 Text 属性
You can bind the Visibility of the TextBlock to the Text property by using a ValueConverter
您可以使用 TextBlock 的触发器来检查 Text 是否为空,并在这种情况下将 Visibility 设置为 Collapsed。
you can use Trigger for TextBlock for checking if Text is null and set the Visibility to Collapsed in that case.