Hi, I want that by default Row 0 will be selected if there are rows in the grid, so I'm using Binding of ActiveDataItem with a property in the MainViewModel (I'm using the MVVM template).
(I want that if the grid is empty and a new line is being added then it will be selected)
However it doesn't always work. On some project it works for the first time, but after a new list is being created then no row is selected although an ActiveDataItem was set.
I've decided to write a small example to show the problem, but in this case Row 0 is never being selected when the collection is being initialized.
I've attached the example solution.
Run it. Now go to the menu and click on New.
As you can see, the "New" creates a new List<SomeClass> and when the first item is being added then the Active Item is being selected as the item on Index 0.
You can see that the labels on the top bind well to the CurrentSomeClass properties.
However, the grid does not select any row.
I think that this is because the ActiveDataItem property is being set before the row was actualy added to the GUI.
If I would for example add all 3 rows and then set the CurrentSomeClass to be item 0 then it works.
If it wrong to do that I'm doing in the code?
Well, if ((sender as BindingList<SomeClass>).Count == 1) happens once,
if ((sender as BindingList<SomeClass>).Count>0) happens more than once if more than 1 line is being added.
If only 1 line will be added then you will still see the problem.
The problem with the solution you gave is that each time a new line is being added, the row marker will jump to the first line.
I'm guessing that the event is happening when a new item was added to the collection but before the grid have added the new row (therefore row 0 cannot be selected).
I've coded a solution. Since I'm adding the items to the collection using a loop (foreach loop) then at the end of the loop it sets the active data item to item 0.
I am not sure why and how exactly right now, but it works correctly with this:
void _SomeClassList_ListChanged(object sender, ListChangedEventArgs e)
{
if (e.ListChangedType == ListChangedType.ItemAdded)
if ((sender as BindingList<SomeClass>).Count>0)
CurrentSomeClass = _SomeClassList[0];
}