Reputation: 37633
I am trying to use code below but it is not working because of the ItemContainerGenerator
var selectedRow = (DataGridRow)myGrid.ItemContainerGenerator.ContainerFromItem(myGrid.SelectedItem);
How to fix it?
Upvotes: 0
Views: 184
Reputation: 6136
You have to pick it from the visual tree
, the DataGrid
offers no convenient access.
var selectedRow = myGrid.GetVisualDescendants()
.OfType<DataGridRow>()
.Where( row => row.DataContext == myGrid.SelectedItem)
.SingleOrDefault();
I recommend you write an extension method for this, it will enhance the code's readability and you can reuse it easily.
Upvotes: 1