Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit How to get DataGridRow from DataGridCell
10 replies. Latest Post by a2005 on June 26, 2009.
(0)
GA30
Member
19 points
41 Posts
12-03-2008 3:43 PM |
Hello All!
I was wondering if anyone can tell me how to get DataGridCell's corresponding DataGridRow? My problem is that I have a datagrid with a number of DataGridTemplateColumns. These columns display a custom usercontrol. Various things happen in the UI that cause the usercontrols to increase and decrease in height. When the usercontrols increase in height, the corresponding datagridrow's height adjusts accordingly and everything is golden. However, if after the usercontrols decrease in height after first having increased in height, the corresponding datagridrow height remains at the higher level and does not decrease in height to fit the usercontrols new lower height. So anyway, I am looking for a workaround. I have an event handler that listens to whenever the usercontrol's size is changed. In this event handler I want to get a reference to the corresponding datarow. But right now at most I am able to get a reference to the datagridcell. Anyone know how to get at the datagridrow?
Thank you
silverbyte
Participant
1338 points
405 Posts
12-03-2008 4:31 PM |
You need to think that your UserControl is a child in a hierarchy of UI elements and one of the parents is the DataGridRow
Look here for a wonderful post regardnig DataGrid layout: http://blogs.msdn.com/vinsibal/archive/2008/08/14/wpf-datagrid-dissecting-the-visual-layout.aspx
You need to parse the elements up to the DataGridRow parent element using VisualTreeHelper:
UserControl_SizeChanged(sender, args)
{
FrameworkElement row = sender as FrameworkElement;
while(!(row is DataGridRow))
row = row.Parent;}
}
12-03-2008 4:53 PM |
Hi silverbyte,
First and foremost, thanks very much for your response!
I hear what you're saying! I tried this but I never reach an item of type DataGridRow. The closest I am getting to is an object of type DataGridFrozenGrid, which I believe is the root element of a the datagridrow's default control template. Any other ideas??????
Thanks!
12-03-2008 4:57 PM |
did you try my code ?
12-03-2008 5:15 PM |
I did! A slightly modified version of it (see below please) because yours was bombing with a nullrefexc
row = row.Parent
12-03-2008 5:41 PM |
DataGridFrozenGrid has as parent the DataGridRow
use Silverlight Spy an excellent tool
12-03-2008 5:46 PM |
again, use VisualTreeHelper:
DependencyObject row =sender as DependencyObject;
while(row != null && !(row is DataGridRow))
row = VisualTreeHelper.GetParent(row);
12-03-2008 6:01 PM |
silverbyte, you da man!
12-03-2008 6:04 PM |
silverbyte, please confirm your answer appears with the symbol showing that it is the right answer. I ask because I've clicked on the "Mark As Answer" link but I don't see the indication that it took it. I may be doing something wrong again
12-03-2008 6:07 PM |
no, it doesn't
just click once and wait for the page to navigate a little
if doesn't work use another browser (should work in IE)
a2005
37 points
39 Posts
06-26-2009 3:37 AM |
More simple
DataGridRow
// elem is the cell or control inside cell