Skip to main content

Microsoft Silverlight

Answered Question How to get DataGridRow from DataGridCellRSS Feed

(0)

GA30
GA30

Member

Member

19 points

41 Posts

How to get DataGridRow from DataGridCell

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
silverbyte

Participant

Participant

1338 points

405 Posts

Re: How to get DataGridRow from DataGridCell

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;
}

}

Please click on 'Mark as answer' near my comment if you feel I answered your question.

GA30
GA30

Member

Member

19 points

41 Posts

Re: How to get DataGridRow from DataGridCell

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!

silverbyte
silverbyte

Participant

Participant

1338 points

405 Posts

Re: Re: How to get DataGridRow from DataGridCell

 did you try my code ?

Please click on 'Mark as answer' near my comment if you feel I answered your question.

GA30
GA30

Member

Member

19 points

41 Posts

Re: Re: How to get DataGridRow from DataGridCell

I did! A slightly modified version of it (see below please) because yours was bombing with a nullrefexc

 

 

FrameworkElement row = sender as FrameworkElement;while (row != null && !(row is DataGridRow))

{

row = row.Parent as FrameworkElement;

}

 

silverbyte
silverbyte

Participant

Participant

1338 points

405 Posts

Re: Re: Re: How to get DataGridRow from DataGridCell

 DataGridFrozenGrid has as parent the DataGridRow

use Silverlight Spy an excellent tool

Please click on 'Mark as answer' near my comment if you feel I answered your question.

silverbyte
silverbyte

Participant

Participant

1338 points

405 Posts

Answered Question

Re: Re: Re: Re: How to get DataGridRow from DataGridCell

 again, use VisualTreeHelper:

DependencyObject row =sender as DependencyObject;

while(row != null && !(row is DataGridRow))

{

        row = VisualTreeHelper.GetParent(row);


Please click on 'Mark as answer' near my comment if you feel I answered your question.

GA30
GA30

Member

Member

19 points

41 Posts

Re: Re: Re: Re: How to get DataGridRow from DataGridCell

silverbyte, you da man!

GA30
GA30

Member

Member

19 points

41 Posts

Re: Re: Re: Re: How to get DataGridRow from DataGridCell

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

silverbyte
silverbyte

Participant

Participant

1338 points

405 Posts

Re: Re: Re: Re: Re: How to get DataGridRow from DataGridCell

 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)

Please click on 'Mark as answer' near my comment if you feel I answered your question.

a2005
a2005

Member

Member

37 points

39 Posts

Re: Re: Re: Re: How to get DataGridRow from DataGridCell

More simple

DataGridRow iRow = DataGridRow.GetRowContainingElement(elem);

 // elem is the cell or control inside cell

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities