Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Get position of child UIElement within its parent. Ignore RenderTransform if any

Lets say I have this XAML code:

<DockPanel Name="pan">
    <Label Content="AAA" Name="lab1" />
    <Label Content="BBB" Name="lab2" />
    <Label Content="CCC" Name="lab3" />
</DockPanel>

I my code behind I want to find out what are the coordinates of lab2 within pan. Hovewer I want to ignore any present RenderTransform of lab2. So the solution must return same coordinates for above code and following:

<DockPanel>
    <Label Content="AAA" />
    <Label Content="BBB" >
        <Label.RenderTransform>
            <TranslateTransform Y="20"/>
        </Label.RenderTransform>
    </Label>
    <Label Content="CCC" />
</DockPanel>

In another words I want the position that was set by the ArrangeOverride method of pan when calling Arrange on but2. I would call it 'logical position'. The 'visual position' can be obtained by calling following method:

private Point visualPoss() {
    Point lab2Vis = lab2.PointToScreen(new Point(0, 0));
    Point panVis = pan.PointToScreen(new Point(0, 0));
    return new Point(lab2Vis.X - panVis.X, lab2Vis.Y - panVis.Y);
} 

But this is not a solution of my problem as the return value of this visualPoss() is not equal for both XAML code examples above.

Please leave comment if somehing is unclear.

Thank you

like image 826
Rasto Avatar asked Jan 21 '23 16:01

Rasto


1 Answers

It looks like there is very simple and clear solution:

private Point logicalPoss() {
    Vector vec = VisualTreeHelper.GetOffset(lab2);
    return new Point(vec.X, vec.Y);
}

It looks like it is working well. If you know abou the scenerio when it will not work please leave a comment.

like image 85
Rasto Avatar answered May 02 '23 03:05

Rasto