WPF listbox item not wordwrapping(WPF列表框项目不自动换行)
问题描述
我的 ListBox
有一个描述字段,其中可能会很长.而不是有一个水平滚动条,我想把它换行.如果我设置 MaxWidth
就可以了,但是由于 ListBox
改变了大小,我不想硬编码这个值.
My ListBox
has, amongst other things a description field in it which can be quite long. Instead of having a horizontal scroll bar I want to word wrap it.
It works if I set the MaxWidth
but since the ListBox
changes size I don't want to hard code the value.
最好的方法是什么?
描述在 TextBlock
中.
简化的 XAML(删除了不必要的东西,仍然显示问题:
Simplified XAML (Removed unnessesary stuff, still shows problem:
<ListBox BorderThickness="0" Padding="5" Name="lstTasks">
<ListBox.ItemsSource>
<Binding Source="{StaticResource dataTasks}"/>
</ListBox.ItemsSource>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=RequestDescription}" TextTrimming="WordEllipsis" TextWrapping="Wrap" Height="60" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
推荐答案
尝试强制 ListBoxItems 的宽度为 ListBox 的宽度:
Try forcing the width of your ListBoxItems to be the width of the ListBox:
<ListBox
Name="lstTasks"
BorderThickness="0"
Padding="5"
HorizontalContentAlignment="Stretch">
您也可以尝试禁用水平滚动:
Also you might try disabling horizontal scrolling:
<ListBox
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
...>
这篇关于WPF列表框项目不自动换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:WPF列表框项目不自动换行


基础教程推荐
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01