none
WPF DataGrid with 1 frozen column - horizontal scroll problem RRS feed

  • Question

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

    XMAL:

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

    C#:

    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 ran in Win7 and Win10.

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

    Monday, January 20, 2020 4:46 AM

All replies

  • Hi Bruce,
    I cannot reproduce your problem with FW 4.8 and Visual Studio 2019.

    XAML:

    <Window x:Class="WpfApp1.Window87"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
            Title="Window87" Height="450" Width="400">
      <Window.DataContext>
        <local:Window87VM/>
      </Window.DataContext>
        <Grid>
        <DataGrid ItemsSource="{Binding View}" FrozenColumnCount="1"/>
      </Grid>
    </Window>

    and ViewModel:

    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Data;
    
    namespace WpfApp1
    {
      public class Window87VM
      {
        public ICollectionView View
        {
          get
          {
            CollectionViewSource cvs = new CollectionViewSource();
            cvs.Source = GetData();
            return cvs.View;
          }
        }
    
        private ObservableCollection<RowOfCells> GetData()
        {
          ObservableCollection<RowOfCells> rowOfCellsList = new ObservableCollection<RowOfCells>();
          for (int i = 1; i < 10; i++)
          {
            rowOfCellsList.Add(new RowOfCells()
            {
              Col00 = $"TextCol00-{i:D2}",
              Col01 = $"TextCol01-{i:D2}",
              Col02 = $"TextCol02-{i:D2}",
              Col03 = $"TextCol03-{i:D2}",
              Col04 = $"TextCol04-{i:D2}",
              Col05 = $"TextCol05-{i:D2}",
              Col06 = $"TextCol06-{i:D2}",
              Col07 = $"TextCol07-{i:D2}",
              Col08 = $"TextCol08-{i:D2}",
              Col09 = $"TextCol09-{i:D2}",
            });
          }
          return rowOfCellsList;
        }
    
        public class RowOfCells
        {
          public string Col00 { get; set; }
          public string Col01 { get; set; }
          public string Col02 { get; set; }
          public string Col03 { get; set; }
          public string Col04 { get; set; }
          public string Col05 { get; set; }
          public string Col06 { get; set; }
          public string Col07 { get; set; }
          public string Col08 { get; set; }
          public string Col09 { get; set; }
        }
      }
    }
    


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Monday, January 20, 2020 6:08 AM
  • Thanks! I will give it a try when I get access to .Net FW 4.8 and VS2019
    Monday, January 20, 2020 9:26 PM
  • What I mean is, a click in the area shown below causes scrolling too far- skipped Col04.  (Col00 is frozen).

    Saturday, January 25, 2020 6:00 AM
  • Hi,
    I recommend you to send feedback (Report a problem) to Microsoft.

    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Saturday, January 25, 2020 8:09 PM
  • Yes, I will do that. I was just wondering if anyone else (in the world) is seeing this same behavior.

    I compiled your code (after changing the x:Class to "WpfApp1.Window87VM" in the XAML) in VS2017 targeting .Net FW 4.8 and ran in Win 10 version 1909 and I see the same (incorrect) behavior.

    Also, it seems to start up just fine, but I'm curious about your lack of a call to InitializeComponent().

    Thanks again.

    -Bruce

    Sunday, January 26, 2020 2:22 AM
  • I have all sorts of problems when I set the FrozenColumnCount to anything other than 0.  On horizontal scroll I get other columns overlaying the frozen one. Also columns that show nothing (a slight horizontal scroll will sometimes restore the content).

    Basically it is really poor implementation.


    Lloyd Sheen

    Sunday, January 26, 2020 1:03 PM