Example of problem:
Framework: WPF Visual control: DataGrid from CodePlex
public Window()
{
InitializeComponent();
var listView = new ListCollectionView(
new[]
{
new
{
Bool = false,
Str = "Value1"
},
new
{
Bool = false,
Str = "Value1"
}
}.ToList());
dataGrid.ItemsSource = listView;
listView.MoveCurrentToFirst();
listView.MoveCurrentToNext();
}
The DataGrid cursor doesn't change position to 1, if change a value of one of anonymous types:
var listView = new ListCollectionView(
new[]
{
new
{
Bool = false,
Str = "Value1"
},
new
{
Bool = false,
Str = "Value2"
}
}.ToList());
Cursor works correct and SelectedIndex = 1.
I think it happens because of anonymous object override GetHashCode() For anonymous object GetHashCode: sum of all fields. If fields are the same for 2 different instances of anonymous objects GetHashCode() will return the same value for both instances.
Maybe DataGrid internally compares objects using GetHashCode and doesn't change SelectedPosition.
Does anybody know how avoid this problem? Assigning anonymous objects to DataGrid is requirement, I can't create strongly typed objects, which means I have to create a wrapper for the object and autogenerate the columns:
public class ViewItemHodler
{
public object ViewItem { get; set; }
}
Thanks