Scrolling WPF DataGrid with the down arrow key acts weird

I have a DataGrid WPF that I have been using for some time and it works great. Unlike the other posters here, I had no problems with the scroll wheel or mouse wheel (for now). I have CTRL END programmed to go to the end of a DataGrid and then it keeps track of the most recently added items. I can scroll the contents of a DataGrid using the up key.

However, I have a really weird behavior with the down key! If I start at the top of my DataGrid and hold down , it scrolls a bit and then eventually bounces back and forth between two adjacent rows. If I pgdn , it will scroll down and then return to the top of the previous two lines between which it will jump, then scroll down to the point at which I pgdn 'd. If I write a few more, the down key will scroll to the end. If I go to the beginning of the DataGrid and start over, I get the same behavior again and again.

I still need to find a message that addresses this, and I have not seen anything in the DataGrid documentation that helps.

These are just three columns of the DataGrid , where TextBlock s is displayed in each column. Can someone explain why this particular scroll mode is problematic? Here is the XAML:

 <DataGrid ItemsSource="{Binding MainLog}" AutoGenerateColumns="False" Name="log_datagrid" SelectedCellsChanged="log_datagrid_SelectedCellsChanged" KeyUp="datagrid_KeyUp" LoadingRow="log_datagrid_LoadingRow"> <DataGrid.Columns> <!-- timestamp --> <DataGridTemplateColumn Header="Timestamp"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Timestamp}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <!-- level --> <DataGridTemplateColumn Header="Level"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Level}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <!-- error message --> <DataGridTemplateColumn Header="Message"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Message}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> 

By the way, this behavior happens even with all my code for event handlers.

Here is the structure definition that my MainLog collection contains:

 public struct MainLogData { public string Timestamp { get; set; } public string Level { get; set; } public string Message { get; set; } } 
+4
source share
1 answer

Ok ... I reproduced the behavior with rows (a simple list of rows bound to a data grid). The behavior started to happen when I entered a list of duplicates in my list. It seems that the data grid is confused between the "selected index" and the "selected value". The same thing happens when I try to select a value (line, in my test) that is present in another visible line: the choice is twisted: half the time, the correct line is not selected.

Your problem is that you are using "struct". A simple solution to your problem is to create a class structure :

 public class MainLogData { public string Timestamp { get; set; } public string Level { get; set; } public string Message { get; set; } } 

Just changing the word struct to a class should fix your problem.

You should understand that structures and classes do not coincide, and structs determine their "equality" to another variable (with the same type) based on the values ​​in them (2 variables of a particular structure type containing the same data will be considered equal). In the case of classes , unless otherwise indicated, equality is determined by the address of its memory; this ensures that by default no 2 instances of an object, even if they contain the same data, are considered equal because they are not on the same memory address (this behavior can be overwritten by overwriting the GetHashCode and Equals methods in any class definition).

Thus, as a result, the DataGrid has problems determining which element you select (or move with the arrow key too), because many objects in your list are considered to be "the same" or "equal." That is why it gets confused. Admittedly, I think this is a data set error (or at least weirder behavior if it is by design), but changing the data type from structure to class should help you get back on track!

Greetings

+6
source

All Articles