The problem of horizontal line alignment in WPF

I use a DataGrid in the form to show a list (user id, name, balance, etc.). There is a very strange problem with it: at the beginning of some lines (one of twenty lines) there is a small white space that causes line offset. White space is like a small rectangle that is placed right in front of the first cell in a row. I looked through my styles and that’s all, but I can’t understand why this is happening. This only happens when I programmatically scroll through my DataGrid, after the scrolling is done, spaces appear.

What's worse is that the empty space can be changed! and when I change it with the mouse, it spins the grid. I really don't want to send something like that.

I do not have much experience in WPF, I would really appreciate it if someone could give me an idea of ​​where to look. Thanks.


Edit: here is a screenshot:

enter image description here

+6
source share
2 answers

This problem is because the row headers are "shown" for some random rows.

I don’t know why this is happening, but fortunately the fix is ​​simple.

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

For some reason, row headers still appear, although the HeadersVisibility parameter HeadersVisibility set to Column.

+7
source

I ran into the same problem for several projects, as far as I can tell, this is just a simple ole error. Which is lame. The only work I found is limping. How super, super lame. I'm sorry that I did not have something better, but I do not. Anyway, I found that when I changed the grid size, these strange scroll artifacts would disappear, so I wrote code to start the mechanism for updating 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(); })); } 
0
source

All Articles