没有列标题高度的Datagrid ActualHeight

时间:2018-09-07 09:45:11

标签: .net wpf datagrid

我试图在不考虑列标题高度的情况下获取WPF Datagrid的ActualHeight。 enter image description here

如何获得?非常感谢

1 个答案:

答案 0 :(得分:1)

您可以通过访问DataGridRowsPresenter中的DataGrid来获得它。喜欢,

var dataGridRowsPresenter = FindVisualChild<DataGridRowsPresenter>(grid);
double actualHeight = dataGridRowsPresenter.ActualHeight;

找到control的可视子级的方法:

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