Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid actual ColumnHeaderHeight

Tags:

c#

wpf

datagrid

When I set a WPF DataGrid's ColumnHeaderHeight to Auto (double.NaN), how do I get the actual rendered height of the column header?

I cannot seem to find the property in the DataGrid class.

like image 471
Jason L Avatar asked May 28 '14 08:05

Jason L


1 Answers

You could get hold of it by searching through the visual tree for the DataGridColumnHeadersPresenter and reading its ActualHeight property.

    var headersPresenter = FindVisualChild<DataGridColumnHeadersPresenter>(dataGrid);
    double actualHeight = headersPresenter.ActualHeight;

Here's the FindVisualChild method. It could be implemented as an extension method as well.

public static T FindVisualChild<T>(DependencyObject current) where T : DependencyObject
{
    if (current == null) return null;
    int childrenCount = VisualTreeHelper.GetChildrenCount(current);
    for (int i = 0; i < childrenCount ; i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(current, i);
        if (child is T) return (T)child;
        T result = FindVisualChild<T>(child);
        if (result != null) return result;
    }
    return null;
}
like image 69
Bartosz Wójtowicz Avatar answered Nov 20 '22 21:11

Bartosz Wójtowicz