1
votes

I have a ListView and in it's ItemTemplate there's a TextBlock (inside a Grid) which often has long lines of text. The problem is if the text is too long it increases the width of the ListViewItem rather than wrapping the text. How can I limit the width of the TextBlock so that it will not exceed the width of the ListView?

I don't want to hardocde the width to a constant value.

I tried setting the ScrollViewer.HorizontalScrollBarVisibility property to Disabled and setting TextWrapping="Wrap" on the TextBlock, but that didn't do the trick. When I debug the application the Live Property Explorer shows that even though the ScrollViewer.HorizontalScrollBarVisibility is disabled it's still horizontally scrollable (the IScrollProvider.HorizontallyScrollable property is true).

Any idea how I can limit the textblock size properly?

2
Have you tried with the MaxWidth property of TextBlock?AarónBC.
Yes, I can limit the width that way, but I don't want to hardcode it to a constant value (the ListView can change it's size when the main window of the application is resized) or bind it to the ActualWidth of the ListView of object. Both are fragile solutions.Bedford

2 Answers

1
votes

If you don't want to hard code the MaxWidth for the text block, just give a relative width to it based on the ListView width.

3
votes

Played with it a little and this gave me the expected result:

<ListView x:Name="listView" 
                  HorizontalAlignment="Stretch" 
                  ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=.}" TextWrapping="Wrap"></TextBlock>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

Hope it works for you too!.