WPF listview vertical scrollbar

wpf

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>

enter image description here
Thanks for help

Best Answer

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.

Related Topic