harpagornis harpagornis - 1 year ago 208
C# Question

Scrolling Below ColumnHeader in WPF Datagrid

I am trying to get this single-column DataGrid to scroll when I hover over the grid and move the mouse wheel.

<Grid Height ="500" .....
<!-- ListCollectionView for sorting -->
<DataGrid Name="dgTable" Style ="{DynamicResource DataGridStyle1}"
Grid.Row ="1" Grid.Column ="0"
ItemsSource="{Binding ItemsLcv}"
Width="215" Height="500">

<DataGridTextColumn Header="Description" SortMemberPath="descr"
Binding="{Binding Path=descr, Mode=TwoWay, UpdateSourceTrigger= PropertyChanged}"
Width ="215"/>

This is the style for the DataGrid.

<Style x:Key="DataGridStyle1" TargetType="DataGrid">
<Setter Property="RowHeaderWidth" Value="0" />
<Setter Property = "ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
<Setter Property = "ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />

This is the Style for the ColumnHeader:

<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Height" Value="35" />
<Setter Property="SeparatorBrush" Value="#79858b" />
<Setter Property="FontWeight" Value="Black" />
<Setter Property="Background" Value ="{DynamicResource Brush1}"/>
<Setter Property="BorderBrush" Value="#FF60727B"/>
<Setter Property="BorderThickness" Value="2,0,2,2"></Setter>

When I placed the DataGrid inside a ScrollViewer tag, the scrolling worked, but the ColumnHeader disapppeared. I need to keep the Columnheader for sorting.

I have reviewed SO posts about frozen rows, Frozen rows in WPF DataGrid?. I was hoping there is a simpler solution than using a lot of control templates on the ScrollViewer components. Thank you in advance.

Answer Source

The DataGrid has its own scrolling that works the way you want.

Try getting rid of the ScrollViewer, and set the DataGrid's VerticalAlignment="Stretch". Not VerticalContentAlignment, that's a different property.

XAML layout is a maze of twisty little properties, all alike, except they're all different.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download