DeveloperInToronto
DeveloperInToronto

Reputation: 1193

WPF datagrid row horizontal alignment issue

I'm using a DataGrid in a form to show a list of (userID, name, balances, etc.). There is a very strange issue with it, at the beginning of some rows (one out of very twenty rows) there is a small white space that is causing that row to be misaligned. The white space is like a little rectangle that is placed right before the first cell of the row. I reviewed my styles and everything but cannot figure out why this is happening. This happens only when I programmatically scroll my DataGrid, after the scroll is done the white spaces appear.

What makes things worse is that the white space is resizable! and when I resize it with my mouse it screws up the grid. I really don't want to ship something like this out.

I don't have much experience in WPF, I would really appreciate it if someone could give me some idea about where to look. Thanks.


Edit: Here is the screenshot:

enter image description here

Upvotes: 7

Views: 1271

Answers (2)

Peter Hansen
Peter Hansen

Reputation: 8907

This issue is caused by row headers being "shown" for some random rows.

I don't know why it happens, but fortunately the fix is simple.

If you set RowHeaderWidth="0" on the DataGrid, the behavior should be as expected.

For some reason the row headers are still making an appearance even though HeadersVisibility is set to Column.

Upvotes: 9

viggity
viggity

Reputation: 15237

I've experienced the same issue on multiple projects, so far as I can tell, it is just a plain ole bug. Which is lame. The only work around I've found is lame. Like super, super lame. I wish I had something better, but I don't. Anyway, I found that when I resized the grid, these weird scrolling artifacts would go away, so I wrote some code to trigger the engine to update the layout.

Timer _timer;

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    _timer = new Timer(1000);
    _timer.Elapsed += _timer_Elapsed;
    _timer.Start();
}

void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
    //why the hell are we doing this?
    //well, the stupid error adorners on the grid like to be mis-aligned
    //forcing a layout to happen makes them not look off. lame, I know.
    Dispatcher.Invoke(new Action(() =>
    {
        MainDataGrid.Margin = new Thickness(MainDataGrid.Margin.Left, MainDataGrid.Margin.Top, MainDataGrid.Margin.Right, MainDataGrid.Margin.Bottom + 1);
        MainDataGrid.UpdateLayout();

        MainDataGrid.Margin = new Thickness(MainDataGrid.Margin.Left, MainDataGrid.Margin.Top, MainDataGrid.Margin.Right, MainDataGrid.Margin.Bottom - 1);
        MainDataGrid.UpdateLayout();
    }));


}

Upvotes: 0

Related Questions