Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF listview vertical scrollbar

Tags:

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

like image 857
user3101007 Avatar asked Jul 27 '15 16:07

user3101007


3 Answers

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.

like image 85
almulo Avatar answered Oct 09 '22 04:10

almulo


Try adding a scrollViewer in DataTemplate like below,

<DataTemplate>
 <ScrollViewer>
   <TextBlock TextWrapping="Wrap" Text="{Binding}"/>
 </ScrollViewer>
</DataTemplate>
like image 4
Abin Avatar answered Oct 09 '22 05:10

Abin


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>  

                            
like image 3
Abdenour Touadi Avatar answered Oct 09 '22 04:10

Abdenour Touadi