Home » c# » c# – WPF DataGrid with 1 frozen column – horizontal scroll problem-Exceptionshub

c# – WPF DataGrid with 1 frozen column – horizontal scroll problem-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have a very simple WPF DataGrid with 1 frozen column. When I scroll horizontally (to the right or left) by clicking in the empty portion of the scrollbar, I scroll too far- probably by the width of the frozen column. (Other ways of scrolling, dragging the thumb, clicking the right or left arrowhead buttons, work fine). I have a few rows and 10 columns, Col00 thru Col09. Col00 is frozen. A click in the area shown in the image causes scrolling too far- skipped Col04.
Click in empty area of horiz. scrollbar moves too far

XAML and C# shown below…

<Window x:Class="TestDatagridScroll.MainWindow"
    xmlns="<the usual URL>"
    xmlns:x="<the usual URL>"
    Title="TestDataGridScroll" Height="198" Width="343">
    <Grid>
        <DataGrid Name="dg" AutoGenerateColumns="True" FrozenColumnCount="1" >
        </DataGrid>
    </Grid>
</Window>
public MainWindow()
{
    InitializeComponent();
    List<RowOfCells> rowOfCellsList = new List<RowOfCells>();
    RowOfCells rowOfCells = null;
    for (int i = 0; i < 20; i++)  //  generate some data rows
    {
        rowOfCells = new RowOfCells();
        rowOfCells.Col00 = $"TextCol00-{i:D2}";
        rowOfCells.Col01 = $"TextCol01-{i:D2}";
        rowOfCells.Col02 = $"TextCol02-{i:D2}";
        rowOfCells.Col03 = $"TextCol03-{i:D2}";
        rowOfCells.Col04 = $"TextCol04-{i:D2}";
        rowOfCells.Col05 = $"TextCol05-{i:D2}";
        rowOfCells.Col06 = $"TextCol06-{i:D2}";
        rowOfCells.Col07 = $"TextCol07-{i:D2}";
        rowOfCells.Col08 = $"TextCol08-{i:D2}";
        rowOfCells.Col09 = $"TextCol09-{i:D2}";
        rowOfCellsList.Add(rowOfCells);
    }
    dg.ItemsSource = rowOfCellsList;
}

RowOfCells is my class of 10 public properties, Col00 thru Col09.

I have built in VS2015 (.Net 4.6.1) and VS2017 (.Net 4.7.2 and .Net 4.8), and ran in Win10 ver.1909.

I have tried many different ways but the behavior is always the same. The behavior is unaffected by the presence of the vertical scrollbar.

If I remove the frozen column then all ways of scrolling are fine, but I need that 1 frozen column.

Is there a fix for this? Or a work-around? Thank you for your time.

How to&Answers: