WPF列表框项目不自动换行 [英] WPF listbox item not wordwrapping

查看:21
本文介绍了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列表框项目不自动换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆