NoWar
NoWar

Reputation: 37633

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?

Upvotes: 0

Views: 184

Answers (1)

Martin
Martin

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

Related Questions