WPF listbox item not wordwrapping

我的未来我决定 提交于 2019-12-04 17:35:44

问题


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>

回答1:


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"
     ...>



回答2:


There isn't meaning of using wrapping and trimming together. You should use one of them texttrimming or textwrapping. It it trims it doesn't wrap.

For textwrapping Matt's answer is correct for trimming you should define width or maxwidth property value.

Its exactly the same for SL developers.



来源:https://stackoverflow.com/questions/1151471/wpf-listbox-item-not-wordwrapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!