ItemContainerGenerator.ContainerFromItem() returns null?

I found something that worked better for my case in this StackOverflow question:

Get row in datagrid

By putting in UpdateLayout and a ScrollIntoView calls before calling ContainerFromItem or ContainerFromIndex, you cause that part of the DataGrid to be realized which makes it possible for it return a value for ContainerFromItem/ContainerFromIndex:

dataGrid.UpdateLayout();
dataGrid.ScrollIntoView(dataGrid.Items[index]);
var row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);

If you don’t want the current location in the DataGrid to change, this probably isn’t a good solution for you but if that’s OK, it works without having to turn off virtualizing.

Leave a Comment