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列表框项目不自动换行
基础教程推荐
- WPF 模态进度窗口 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
