How do I wrap content in a WPF ListView?
listview, wpf
Solution
This should be what you need
<ListView Margin="12,23,309,191"
Name="mLogListView"
FontWeight="Bold"
FontSize="16"
SelectionMode="Single"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
HorizontalContentAlignment="Stretch" >
<!-- here set the itemTemplate to a TextBlock that can wraps-->
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=.}" TextWrapping="Wrap"></TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Note the syntax `Text="{Binding Path=.}"` that is equivalent to `Text="{Binding}"`. This is called empty binding syntax.
In this case `Text` is bound to the entire ListViewItem object. The empty binding syntax is useful when you want to bind to the entire object item instead of just to single property of the item.
This is convenient for the example because the source object (the ListViewItem) is of type string and you simply want to bind to the string itself.
For more information see msdn at section Specifying the Path to the Value
Problem
I have a very simple WPF ListView that I'm using to list blocks of text. I'd like it to scroll vertically, but the text should wrap so that there is no horizontal scroll. All the examples I've seen are overly convoluted DataGridView nesting solutions. This seems like such a simple use-case, however. Here is my current code: ``` <ListView Height="Auto" Width="Auto" Margin="0" Name="mLogListView" FontWeight="Bold" FontSize="16" SelectionMode="Single" ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalContentAlignment="Stretch"/> ``` I've tried setting the ScrollViewer.HorizontalScrollBarVisibility and HorizontalContentAlignment properties but the text simply runs off the end of the control and doesn't wrap. Each item is added to the `ListView.Items` collection and is a `ListViewItem` object. The text is set to the item's `Content` property. Here is the code responsible for adding text times to the list: ``` ListViewItem item = new ListViewItem(); item.Content = "Item text is set here, but refuses to wrap in list view!"; mLogListView.Items.Add(item); ``` Thank you.