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

查看:737
本文介绍了WPF列表框的项目不自动换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的列表框有(​​其中包括)中有一个描述字段可能相当长。而不是有水平滚动条我想自动换行了。 它的工作原理,如果我设置了了maxWidth 但由于列表框更改大小我不想硬code值

什么是做到这一点的最好方法是什么?

编辑:该说明是在的TextBlock

简体XAML(去掉unnessesary的东西,仍然显示问题:

 <列表框了borderThickness =0填充=5NAME =lstTasks>
            < ListBox.ItemsSource>
                <绑定源={的StaticResource dataTasks}/>
            < /ListBox.ItemsSource>

            < ListBox.ItemTemplate>
                <的DataTemplate>
                    <电网>
                        < TextBlock的文本={绑定路径= RequestDescription}的TextTrimming =WordEllipsisTextWrapping =自动换行高度=60/>
                    < /网格>
                < / DataTemplate中>
            < /ListBox.ItemTemplate>
        < /列表框>
 

解决方案

试着强迫你的ListBoxItems的宽度为列表框的宽度:

 <列表框
     NAME =lstTasks
     了borderThickness =0
     填充=5
     Horizo​​ntalContentAlignment =拉伸>
 

你也可以尝试禁用水平滚动:

 <列表框
     ScrollViewer.Horizo​​ntalScrollBarVisibility =已禁用
     ...>
 

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.

What's the best way to do this?

EDIT: The description is in a TextBlock.

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>

解决方案

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天全站免登陆