15

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

1
  • what type of container is wrapped around the ListView?
    – Breeze
    Commented Jul 27, 2015 at 16:45

4 Answers 4

31

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.

0
5

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>  

                            
3

Try adding a scrollViewer in DataTemplate like below,

<DataTemplate>
 <ScrollViewer>
   <TextBlock TextWrapping="Wrap" Text="{Binding}"/>
 </ScrollViewer>
</DataTemplate>
2
  • 1
    Doesn't work for me. Besides, ff there are more than one item, and each of them will have a scrollviewer around, it will become messy and ugly, don't you think so? Commented Jul 27, 2015 at 17:51
  • 2
    It's not a question of a template. It's a question, that this template will be used for all items in a listview - so 50 items - 50 scrollviwers for each of them, Not something anyone wants to see Commented Jul 27, 2015 at 18:00
-1

Wrap the ListView in a DockPanel.

2
  • I did not contribute a negative rating to this answer. But, to explain why someone else did, a DockPanel is not available to many use cases, possibly also not for the OP. There must be better ways to solve the problem. Commented Aug 3, 2023 at 12:44
  • 1
    That's allright, I'm not counting. My suggestion is based on code that actually works in my case, so somebody that google their way here may find this useful regardless of the count. Commented Aug 4, 2023 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.