查找datagridcolumn的根元素
-
12-10-2019 - |
题
我正在使用 LogicalTreeHelper.GetParent()
递归地找到其他各种WPF元素的根元素。这几乎可以使用所有内容,但是DataGridColumn失败了,例如 DataGridTextColumn
。我发现 DataGridColumn
不是逻辑树的一部分,也不是视觉树。我可以以某种方式找到 DataGrid
它属于(然后从网格中获取根)?
阅读MSDN文档,我找不到合适的解决方案。谢谢你。
我的代码找到逻辑根:
private DependencyObject FindLogicalRoot(DependencyObject obj)
{
if (obj == null)
return null;
else
{
var parent = LogicalTreeHelper.GetParent(obj);
return parent != null ? FindLogicalRoot(parent) : obj;
}
}
解决方案
DataGridColumn具有此属性,但它是私人的,因此您必须使用反射来获得它。要么在VisualTree中进行一些搜索,然后将每个数据杂志的列与要查找的列进行比较
public DataGrid GetDataGridParent(DataGridColumn column)
{
PropertyInfo propertyInfo = column.GetType().GetProperty("DataGridOwner", BindingFlags.Instance | BindingFlags.NonPublic);
return propertyInfo.GetValue(column, null) as DataGrid;
}
其他提示
var grid = ((Telerik.Windows.Controls.GridView.GridViewCellBase)
((sender as FrameworkElement).Parent)).Column.DataControl;
不隶属于 StackOverflow