Reputation: 750
I've got a listview control with a gridview as it's view property. One of it's columns is dedicated to display a really long text. That column cell template is set to a TextBlock. Whenever listview item source contains only one item, no matter that mentioned TextBlock content starts to exceed listview height, vertical scroll is unavailable.
<ListView ItemsSource="{Binding Path=ReportEntries}" VerticalContentAlignment="Top" ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="VerticalContentAlignment" Value="Top"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Path}" />
<GridViewColumn Header="Message" Width="50" DisplayMemberBinding="{Binding Message}" />
<GridViewColumn Header="Details" DisplayMemberBinding="{Binding Details}" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
Upvotes: 15
Views: 52256
Reputation: 51
Yous just need to limit the height of your ListView with MaxHeight propertie Exemple:
<ListView ItemsSource="{Binding Path=InkersList}"
MaxHeight="400">
<ListView.Resources>
<DataTemplate DataType="{x:Type VM:InkerVM}">
<Views:InkerView />
</DataTemplate>
</ListView.Resources>
</ListView>
Upvotes: 5
Reputation: 4978
Set ScrollViewer.CanContentScroll="False"
on your ListView
.
<ListView ItemsSource="{Binding Path=ReportEntries}"
VerticalContentAlignment="Top"
ScrollViewer.VerticalScrollBarVisibility="Visible"
ScrollViewer.CanContentScroll="False">
<!-- etc. -->
</ListView>
The ListView
is trying to virtualize, by default. Virtualizing means, among other things, using logical scroll. Logical scrolling means the ScrollViewer scrolls item by item, jumping from one item to another instead of smoothly scrolling through the whole extent of each item.
By setting CanContentScroll
to false
, you're telling the ScrollViewer
to stop using logical scrolling, which also deactivates virtualization.
If you're gonna show LOTS of items in that ListView
, maybe virtualization is a big deal and this will introduce a performance issue.
If you're just gonna show a handful of items, then this should be completely fine.
EDIT - If performance is indeed an issue and you need to keep virtualization activated, consider setting a fixed height for all rows and adding its own ScrollViewer
around the TextBlock
with the big text.
Upvotes: 31
Reputation: 2956
Try adding a scrollViewer in DataTemplate like below,
<DataTemplate>
<ScrollViewer>
<TextBlock TextWrapping="Wrap" Text="{Binding}"/>
</ScrollViewer>
</DataTemplate>
Upvotes: 3