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列表框项目不自动换行


- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- WebMatrix WebSecurity PasswordSalt 2022-01-01