harpagornis harpagornis - 2 months ago 20
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"
VerticalContentAlignment="Stretch"
ItemsSource="{Binding ItemsLcv}"
IsSynchronizedWithCurrentItem="True"
SelectionMode="Single"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserSortColumns="True"
HorizontalAlignment="Center"
Width="215" Height="500">

<DataGrid.Columns>
<DataGridTextColumn Header="Description" SortMemberPath="descr"
IsReadOnly="True"
CanUserSort="True"
Binding="{Binding Path=descr, Mode=TwoWay, UpdateSourceTrigger= PropertyChanged}"
Width ="215"/>
</DataGrid.Columns>


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" />
</Style>


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


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

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.