CodeGo.net> WPF的DataGrid的实际ColumnHeaderHeight

当我将WPF DataGrid的ColumnHeaderHeight设置为Auto(double.NaN)时,如何获取列标题的实际呈现高度?

我似乎在DataGrid类中找不到该属性.

解决方法:

您可以通过在可视树中搜索DataGridColumnHeadersPresenter并读取其ActualHeight属性来掌握它.

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

这是FindVisualChild方法.它也可以实现为扩展方法.

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;
}
上一篇:ASP.NET Aries 高级开发教程:行内编辑事件怎么新增数据到后台(番外篇)


下一篇:EasyUI DataGrid 通过index获取行的内容