简体   繁体   中英

Get row in Silverlight DataGrid

I am trying to use code below but it is not working because of the ItemContainerGenerator

  var selectedRow = (DataGridRow)myGrid.ItemContainerGenerator.ContainerFromItem(myGrid.SelectedItem);

Is this only one solution?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM