ScrollViewer not enabled for ListView

2019-05-02 17:00发布

问题:

I have a ListView control inside UserControl. But when content overflows size of the ListView, vertical ScrollBar is not enabled, despite of setting it in XAML.

XAML appears as follows:

<UserControl x:Class="GrandSuccessProject.View.ContactsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="367" d:DesignWidth="548" Background="{x:Null}" VerticalContentAlignment="Top">

<ListView ScrollViewer.CanContentScroll="True" 
          ScrollViewer.VerticalScrollBarVisibility="Visible" 
          ItemsSource="{Binding SelectedContacts}"  
          ItemContainerStyle="{StaticResource ContainerStyle}" 
          Grid.Row="1" 
          VerticalAlignment="Top" 
          VerticalContentAlignment="Top" />      

</UserControl>

I also tried grouping the ListView inside a ScrollViewer, but still doesn't work.

Thank you very much in advance :)

回答1:

This looks like

  1. The ListView taking all the space it needs for all the items, hence the scrolling is disabled.
  2. The ListView exceeding its container's bounds.

So i would assume that the container is at fault for not restricting the size of the ListView, where did you place it? Make sure the container lays out the controls with limitations.



回答2:

I Solved it passing the Height and Width from the MainWindow to the Usercontrol:

 <UC:UC Width="{Binding ElementName=GridMainWindow, Path=ActualWidth}" 
    Height="{Binding ElementName=GridMainWindow, Path=ActualHeight}"/>

And inside the usercontrol i defined a Gridview like this:

<Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" /> <!--This inherith from the window-->
</Grid.ColumnDefinitions>