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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.