Vertical scroll bar does not appear when grid items exceed display height
I place the following statements on the second line of my grid in the 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 have bound listView.ItemSource to an ObservableCollection defined in code to populate the data in the list. When the number of items added to the GridView exceeded the height of the list, the vertical scrollbar was not displayed as I indicated in the XAML. What have I done wrong? Your input is greatly appreciated. Thank.
This 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 control template ListView
contains ScrollViewer
already one that ScrollViewer
will appear inside ListView
as needed. Why do you need to wrap it in another?
source to share
Don't need to use ScrollViewer
. Just remove ScrollViewer
and use only ListView
and try.
ListView listView = new ListView();
listView.SetValue(Grid.RowProperty, 1);
listView.SetValue(Grid.ColumnProperty, 1);
MainGrid.Children.Add(listView);
You don't need to specify the width and height of the list.
source to share
<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>
source to share