Header & ScrollBar Height

Nov 26, 2008 at 7:44 PM
Edited Nov 26, 2008 at 7:45 PM
I can get the Grid height and RowHeight

grid.ActualHeight
DataGridRow dr = (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.Items[0]);
dr.ActualHeight;

But how do I get the Header & ScrollBar Height?

These both return null

grid.Template.FindName("PART_ColumnHeadersPresenter", grid);
grid.Template.FindName("PART_ScrollContentPresenter", grid);

 

 

 

Coordinator
Nov 27, 2008 at 2:26 AM
grid.Template.FindName("PART_ColumnHeadersPresenter", grid);
grid.Template.FindName("PART_ScrollContentPresenter", grid);

would return null because these are not directly inside the template DataGrid. The template of DataGrid has only a ScrollViewer which itself is retemplated to contain all of these.

Following code should work for you....

                    DataGridColumnHeadersPresenter chp = null;
                    Control sv = dataGrid.Template.FindName("DG_ScrollViewer", dataGrid) as Control;
                    if (sv != null)
                    {
                        chp = sv.Template.FindName("PART_ColumnHeadersPresenter", sv) as DataGridColumnHeadersPresenter;
                    }

and similar code for scroll content presenter too.
Nov 27, 2008 at 7:07 PM
Thankyou!!!

That worked great!

Turns out all I needed was the PART_ScrollContentPresenter, but here are the others in case someone else needs them.

DataGridColumnHeadersPresenter chp = null;
ScrollContentPresenter                   scp = null;
ScrollBar                                          hsb = null;
Control sv = grid.Template.FindName("DG_ScrollViewer", grid) as Control;
if (sv != null)
  {
  scp = sv.Template.FindName("PART_ScrollContentPresenter", sv) as ScrollContentPresenter;
  chp = sv.Template.FindName("PART_ColumnHeadersPresenter", sv) as DataGridColumnHeadersPresenter;
  hsb = sv.Template.FindName("PART_HorizontalScrollBar", sv) as ScrollBar;
  }