Wpf 获取相对于DataGrid顶部的DataGridRow顶部的垂直偏移

Wpf 获取相对于DataGrid顶部的DataGridRow顶部的垂直偏移,wpf,datagrid,Wpf,Datagrid,如何从可见的DataGridRow的Top获取相对于DataGrid本身的Top的垂直偏移 明确地说,我不是在ScrollViewer中查找行的垂直偏移量 我希望在加载附加行为中的行时检索此信息,但不确定如何检索 private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e) { DataGrid dg = this.AssociatedObject; DataGridRow dgr = e.Row;

如何从可见的
DataGridRow
Top
获取相对于
DataGrid
本身的
Top
的垂直偏移

明确地说,我不是在
ScrollViewer
中查找行的垂直偏移量

我希望在加载附加行为中的行时检索此信息,但不确定如何检索

private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
    DataGrid dg = this.AssociatedObject;
    DataGridRow dgr = e.Row;
}

您可以使用
TranslatePoint
方法:

private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
    DataGrid dg = this.AssociatedObject;
    DataGridRow dgr = e.Row;

    Point p = dgr.TranslatePoint(new Point(0, 0), dg);
    double verticalDistance = p.Y;
    //...
}