WPF:其父项内获取孩子的UIElement的地位。忽略的RenderTransform如果有的话 [英] WPF: Get position of child UIElement within its parent. Ignore RenderTransform if any

查看:487
本文介绍了WPF:其父项内获取孩子的UIElement的地位。忽略的RenderTransform如果有的话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有这个XAML code:

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>

我我的code的背后,我想找出什么是 LAB2 的坐标。 Hovewer 我想忽略 LAB2 的任何present的RenderTransform。因此,解决方案必须返回相同的坐标,上面code和以下内容:

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>

在另一个的话我想这是由 ArrangeOverride 方法调用<$ C时,设定的位置$ C>排列在 but2 。我把它叫做逻辑位置。在视觉位置可以通过调用下面的方法获得:

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

不过这不是我的问题的解决方案,这个返回值 visualPoss()不等于为XAML code以上的例子。

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.

请留下评论,如果somehing目前还不清楚。

Please leave comment if somehing is unclear.

感谢您

推荐答案

它看起来像有是非常简单和明确的解决方案:

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.

这篇关于WPF:其父项内获取孩子的UIElement的地位。忽略的RenderTransform如果有的话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆