当gridview项目超出显示高度时,不会出现垂直滚动条

时间:2008-11-10 07:55:23

标签: wpf gridview listview scrollviewer

我将以下语句放在xaml的网格的第二行中:

<ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="1">
     <ListView Name="listView" Margin="5" Grid.Row="1">

                <ListView.View>
                    <GridView AllowsColumnReorder="True">
                        <GridViewColumn DisplayMemberBinding="{Binding Path=DateTime}" Header="Date Time" Width="140"/>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=Vehicle}" Header="Vehicle" Width="130"/>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=AlarmType}" Header="Alarm Type" Width="100"/>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=Direction}" Header="Direction" Width="100"/>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=Speed}" Header="Speed" Width="100"/>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=Alarmed}" Header="Alarmed" Width="100"/>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=LoadType}" Header="Load Type" Width="100"/>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=Status}" Header="Status" Width="110"/>
                    </GridView>
                </ListView.View>
            </ListView>     
        </ScrollViewer>
 </Grid>

我将listView.ItemSource绑定到代码中定义的ObservableCollection,以将数据填充到列表中。当添加到GridView的项目数超过列表视图高度时,垂直滚动条没有像我在XAML中指定的那样出现。我做错了什么?非常感谢您的意见。谢谢。

6 个答案:

答案 0 :(得分:6)

它对我有用:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="1">
            <ListView Name="listView" Margin="5" Grid.Row="1">

                <ListView.View>
                    <GridView AllowsColumnReorder="True">
                        <GridViewColumn DisplayMemberBinding="{Binding Path=.}" Header="Whatever" Width="140"/>
                    </GridView>
                </ListView.View>
            </ListView>
        </ScrollViewer>
    </Grid>
</Window>

但是,ListView控件模板已经包含ScrollViewerScrollViewer将在ListView内显示,并在需要时显示。为什么需要将它包装在另一个?

答案 1 :(得分:3)

看到边距和填充是正确的。滚动条可以在后面。

将外部容器高度设置为固定值,它可以拉伸列表视图,因此它永远不会显示滚动条。

HTH

答案 2 :(得分:1)

试试这段代码:

ListView listView = new ListView();
listView.SetValue(Grid.RowProperty, 1);
listView.SetValue(Grid.ColumnProperty, 1);
MainGrid.Children.Add(listView);

答案 3 :(得分:0)

无需使用ScrollViewer。只需删除ScrollViewer并仅使用ListView并尝试。

ListView listView = new ListView();
listView.SetValue(Grid.RowProperty, 1);
listView.SetValue(Grid.ColumnProperty, 1);
MainGrid.Children.Add(listView);

无需指定列表视图的宽度和高度。

答案 4 :(得分:0)

<Grid x:Name="MainMenuButtonGrid">
  <StackPanel Margin="50,0,0,0">
    <TextBlock Text="Please select any employee" Foreground="Wheat"/>
    <ListView x:Name="listEmployeeDetail" SelectedValuePath="EmployeeID">
      <ListView.View>
        <GridView>
          <GridViewColumn Header="EmployeeName" Width="100" DisplayMemberBinding="{Binding EmployeeName}"></GridViewColumn>
        </GridView>
      </ListView.View>
    </ListView>
  </StackPanel>
</Grid>

答案 5 :(得分:0)

您可以简单地使用 MaxHeight 属性将您的列表视图限制为特定高度,滚动条将自动出现。 例如:

 <ListView Name="listView" Margin="5" Grid.Row="1" MaxHeight="300">
            <ListView.View>
                <GridView AllowsColumnReorder="True">
                    <GridViewColumn DisplayMemberBinding="{Binding Path=DateTime}" Header="Date Time" Width="140"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Vehicle}" Header="Vehicle" Width="130"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=AlarmType}" Header="Alarm Type" Width="100"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Direction}" Header="Direction" Width="100"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Speed}" Header="Speed" Width="100"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Alarmed}" Header="Alarmed" Width="100"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=LoadType}" Header="Load Type" Width="100"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Status}" Header="Status" Width="110"/>
                </GridView>
            </ListView.View>
        </ListView>