In WinForm DataGridView, it automatically selects the first row when initialized. It drove me crazy when I tried to turn that feature off. Moving to WPF DataGrid, it seems M
I'm glad to report I found a solution for this problem through ItemContainerGenerator.StatusChanged
event.
dataGrid.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);
void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
if (dataGrid.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
{
dataGrid.SelectedIndex = 0;
}
}
It looks when this event is fired with status ContainersGenerated, dataGrid is fully initialized. To me, this is more like DataGridView's DataBindingComplete event in WinForm. If so, the "DataContextChanged
" event should really be called "DataContextChanging
" event.
This was inspired by a post here I accidently found while looking for another clue.