wpf:当文本对于 1 行来说太大时,使文本块高度扩展
我有一个带有项目模板的列表视图:
<ListView x:Name="messages" HorizontalAlignment="Left"
Background="{x:Null}" BorderBrush="{x:Null}" Foreground="Black">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Style="{DynamicResource h3}" Text="{Binding}"
Margin="10" MaxWidth="850"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
此列表视图位于垂直堆栈面板中。所以它的宽度与 stackpanel 的宽度相同。
列表视图必须显示可能很长的消息。我试图确保当消息对于可用宽度来说太长时,文本块会获得额外的高度并且文本会显示在两行上。
我可以通过谷歌搜索很多方法来实现固定高度,但由于我事先不知道是否需要超过 1 行,所以我想确保它自动发生。 我不希望每个项目都有 2 行的高度,只有在需要时才这样做。
我怎样才能实现这个目标?
I have a listview with an itemtemplate:
<ListView x:Name="messages" HorizontalAlignment="Left"
Background="{x:Null}" BorderBrush="{x:Null}" Foreground="Black">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Style="{DynamicResource h3}" Text="{Binding}"
Margin="10" MaxWidth="850"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
This listview is in a vertical stackpanel. So its width is the same as the stackpanel's width.
The listview must show messages that could be very long. I'm trying to make sure that when a message is too long for the available width, the textblock gets extra height and the text gets displayed on 2 lines.
I can google a lot of ways to have this achieved with a fixed height, but since I don't know in advance if I'll need more than 1 line, I'd like to make sure it happens automaticly.
I don't want every item to have the height of 2 lines, only when it's needed.
How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试过 TextWrapping 属性吗?看起来它会做你想做的事。
Have you tried the TextWrapping property? It seems that it would do what you want.