Can i reference to DataGridRow on DataGrid

Feb 18, 2009 at 8:11 AM
I want to know how to reference to DataGridRow on DataGrid like dg.Rows[index]. Can I do this on WPF Toolkit DataGrid?

Thanks,
Ek
Feb 18, 2009 at 8:40 AM
Hi Ek,

This will do the trick:

dg.ItemContainerGenerator.ContainerFromIndex(index);

Not all that obvious is it?

The DataGrid is an ItemsControl (just like LiistView, ListBox etc ...), each ItemsControl has an associated ItemContainerGenerator which is responsible for generating the 'container' for your bounds data object. For the DataGrid, the container is a DataGridRow.

Have a look at the other methods on dg.ItemContainerGenerator, you might find them useful in the future.

Regards,
Colin E.
--
http://www.scottlogic.co.uk/blog/wpf/ - my WPF blog
http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx - WPF DataGrid Practical Examples

Feb 19, 2009 at 12:17 AM
I tried this, but when I attempted to pull off things like height, I got nothing.  Are there any other solutions?
Feb 19, 2009 at 12:35 AM
Thank you Colin, Did you have any suggestion book for WPF developer? (I have start half of WPF Unleashed)

Thanks,
Ek
Feb 19, 2009 at 8:40 AM
I have read a few WPF books and in my opinion WPF Unleashed is by far the best - so keep going with it!

Colin E.
Feb 19, 2009 at 3:46 PM
Colin, do you think you can give an example on how to do some direct DataGridRow manipulation?

Like this:
((DataGridRow)((DataGrid)sender).ItemContainerGenerator.ContainerFromItem((Objective)e.AddedItems[0])).Height

but that doesn't work...
Feb 19, 2009 at 5:09 PM
Resolved!  I found out that I needed to get the actual height, because Height is just a place holder to tell it what Height to set it at:
((DataGridRow)((DataGrid)sender).ItemContainerGenerator.ContainerFromItem((Objective)e.AddedItems[0])).ActualHeight

This does return the clicked row on this event:
private void SelectedRow(object sender, SelectionChangedEventArgs e)