wpf gridview项超过显示高度时不出现垂直滚动条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/277316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:46:11  来源:igfitidea点击:

Vertical scroll bar does not appear when the gridview items exceeded the display height

wpfgridviewlistviewscrollviewer

提问by Artur Carvalho

I place the following statements in the second row of my grid in the xaml:

我将以下语句放在 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>

I binded the listView.ItemSource to an ObservableCollection defined in the code to populate data to the list. When the number of items added to the GridView exceeded the listview height, the vertical scroll bar did not appear as i specified in the XAML. What did I do wrong? Your input is greatly appreciated. Thank you.

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

回答by Kent Boogaart

It works for me:

这个对我有用:

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

However, the ListViewcontrol template contains a ScrollVieweralready such that the ScrollViewerwill appear insidethe ListViewas and when needed. Why do you need to wrap it in another?

然而,ListView控制模板包含一个ScrollViewer已经使得ScrollViewer会出现内部ListView作为并在需要时。为什么你需要把它包装在另一个?

回答by Artur Carvalho

See that the margins and paddings are correct. The scrollbar can be behind something.

查看边距和填充是否正确。滚动条可以在某物后面。

Put the exterior container height with a fixed value, It can be stretchingthe listview so it will never show the scrollbar.

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

HTH

HTH

回答by lincy oommen

Try this code:

试试这个代码:

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

回答by Praveen Chandran

No need of using ScrollViewer. Just remove the ScrollViewerand use only the ListViewand try.

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

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

No need of specifying the width and height for the listview.

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

回答by ankit

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