FindElementsInHostCoordinates相对于控制空间不是整页

bea*_*ous 5 silverlight visualtreehelper silverlight-3.0

我正在使用VisualTreeHelper方法FindElementsInHostCoordinates在给定的X和Y位置查找ListBoxItem.但是,X和Y值似乎与整个页面中的点有关,而不仅仅是我感兴趣的ListBox(即使我将该元素传递给方法的子树参数).下面,这是指从ListBox派生的自定义控件.

foreach (UIElement element in VisualTreeHelper.FindElementsInHostCoordinates(new Point(X, Y), this))
{
    if (element is ListBoxItem)
    {
        int index = this.ItemContainerGenerator.IndexFromContainer(element);
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,(0,0)将相对于整个插件的左上角而不是ListBox的左上角.我是否需要在这里(在代码中)进行一些数学工作以将页面坐标转换为ListBox坐标,或者是否有其他方法来执行命中测试以判断给定的X和Y点是否在ListBoxItem上?

谢谢.

bea*_*ous 9

我自己想出来了(哇).我确定了控件的顶部和左侧值,然后在控件内部添加了X和Y值:

GeneralTransform gt = this.TransformToVisual(Application.Current.RootVisual as UIElement);
Point offset = gt.Transform(new Point(0, 0));
double controlTop = offset.Y + Y;
double controlLeft = offset.X + X;
Run Code Online (Sandbox Code Playgroud)

所以,我可以传递相对于我的控件的X和Y值,但是这段代码会将它转换为全局坐标.

适合我和嘿!在这个问题上没有风滚草徽章.:)