ScrollViewer未启用ListView

时间:2012-01-30 15:42:44

标签: c# wpf xaml listview scrollview

我在ListView内有一个UserControl控件。但是,当内容溢出ListView的大小时,垂直ScrollBar未启用,尽管在XAML中设置了它。

enter image description here

XAML如下所示:

<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>

我还尝试将ListView分组到ScrollViewer内,但仍无效。

非常感谢您提前:))

2 个答案:

答案 0 :(得分:6)

这看起来像

  1. ListView占用了所有项目所需的所有空间,因此滚动被禁用。
  2. ListView超出其容器的界限。
  3. 所以我假设容器因为没有限制ListView的大小而出错,你把它放在哪里?确保容器有限制地布置控件。

答案 1 :(得分:0)

我解决了将高度和宽度从MainWindow传递到Usercontrol的问题:

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

在用户控件中,我定义了一个Gridview,如下所示:

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