当我将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;
}