WPF DataGrid actual ColumnHeaderHeight

c#, datagrid, wpf

Solution

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

Problem

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.

Original source