One possible reason for the ItemContainerGenerator.ContainerFromItem()
method to stop working is for the SelectedItem
to be null
. You need a SelectedItem
otherwise you won't get its container.
Another way to get the grouped row is to check if there are any groups and then use the ChildrenOfType<GridViewRow>()
extension method on the GridView
to get the selected row container. However, this will only work if the GroupRenderMode
is set to Nested
(this is the default mode), if its set to Flat
you can use the ItemContainerGenerator.ContainerFromItem(grid.SelectedItem)
method to get the selected container row container.
sender, RoutedEventArgs e)
var grid =
(grid.GroupCount > 0 && grid.SelectedItem !=
(grid.GroupRenderMode == GroupRenderMode.Nested)
var groupedRows = grid.ChildrenOfType<GridViewRow>().FirstOrDefault(x => x.IsSelected);
// If GroupRenderMode="Flat" use this
var rowContainer = grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem)
For more information about ChildrenOfType
() method you can check Easy programmatic UI customization for WPF and Silverlight
In order to advise you better could you please provide us with more details about your scenario ?
We are looking forward to your response.
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos
and start improving your app based on facts, not hunches.