画布内的 TranslatePoint

发布于 2024-08-16 07:34:38 字数 1105 浏览 2 评论 0原文

我的应用程序中有一个滚动查看器,其中包含一个画布,其中有一个自定义绘制的树结构。我试图获取画布中特定节点元素相对于滚动查看器的位置(以便我可以滚动到它),但我的尝试不起作用。

我尝试过使用 marker.TranslatePoint(new Point(0, 0),scrollViewer) (其中 marker 是画布中的元素),但这只是返回画布的位置,而不是标记的位置。同样,如果我尝试 marker.TranslatePoint(new Point(0, 0), layoutCanvas),我总是会得到 (0,0) 作为结果,无论在哪里标记实际上是。

这是我的代码:

var marker = m_Metadata[node].Marker;
var location = marker.TranslatePoint(new Point(0, 0), scrollViewer); // This inorrectly gives the position of the canvas, rather than the marker.
var size = new Size(marker.Width, marker.Height);
var markerArea = new Rect(location, size);

double horizontalOffset = (markerArea.Right + markerArea.Left - scrollViewer.ViewportWidth) / 2;
double verticalOffset = (markerArea.Bottom + markerArea.Top - scrollViewer.ViewportHeight) / 2;

我还尝试使用 marker.TransformToVisual(scrollViewer).Transform(new Point(0, 0),但这给出了相同的结果。

我可以使用 解决它>Canvas.GetLeftCanvas.GetTop 等,但这很混乱且令人费解(因为它们并不总是左对齐和上对齐),

我该如何解决这个问题,或者它只是不适用于画布?

I have a scroll viewer in my application that contains a canvas, in which I have a tree custom-drawn tree structure. I'm trying to get the position of a particular node element in the canvas relative to the scroll viewer (so I can scroll to it), but my attempts aren't working.

I've tried using marker.TranslatePoint(new Point(0, 0), scrollViewer) (where marker is the element in the canvas), but this is just returning the position of the canvas, rather than the marker. Similarly, if I try marker.TranslatePoint(new Point(0, 0), layoutCanvas), I invariably just get (0,0) as the result, regardless of where the marker actually is.

Here's my code:

var marker = m_Metadata[node].Marker;
var location = marker.TranslatePoint(new Point(0, 0), scrollViewer); // This inorrectly gives the position of the canvas, rather than the marker.
var size = new Size(marker.Width, marker.Height);
var markerArea = new Rect(location, size);

double horizontalOffset = (markerArea.Right + markerArea.Left - scrollViewer.ViewportWidth) / 2;
double verticalOffset = (markerArea.Bottom + markerArea.Top - scrollViewer.ViewportHeight) / 2;

I've also tried using marker.TransformToVisual(scrollViewer).Transform(new Point(0, 0), but this gives the same results.

I can work around it by using Canvas.GetLeft, Canvas.GetTop et al, but this is messy and convoluted (since they won't always be left- and top-aligned).

How can I fix this, or does it just not work for canvases?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素食主义者 2024-08-23 07:34:38

您是否尝试过使用VisualTreeHelper.GetOffset(Visual obj)?我不在 Studio 前面,但我似乎记得过去曾用它来做过类似的事情......

Have you tried using VisualTreeHelper.GetOffset(Visual obj)? I'm not in front of Studio, but I seem to recall using that for just this sort of thing in the past...

沫雨熙 2024-08-23 07:34:38

我为这个问题编写了一个简单的解决方法。它有效,但不是很干净,所以如果有人对此有答案,我仍然会很感激!这是我当前使用的代码:

private static Point GetCanvasChildPosition(FrameworkElement element)
{
    var canvas = element.Parent as Canvas;

    double left = Canvas.GetLeft(element);
    double top = Canvas.GetTop(element);

    bool isLeftAligned = !double.IsNaN(left);
    bool isTopAligned = !double.IsNaN(top);

    double x;
    double y;

    if (isLeftAligned)
    {
        x = left;
    }
    else
    {
        x = canvas.Width - Canvas.GetRight(element) - element.Width;
    }

    if (isTopAligned)
    {
        y = top;
    }
    else
    {
        y = canvas.Height - Canvas.GetBottom(element) - element.Height;
    }

    return new Point(x, y);
}

I've written a simple workaround method for this problem. It works, but it's not very clean, so if anyone has an answer to this I'd still appreciate it! Here's the code I'm currently using:

private static Point GetCanvasChildPosition(FrameworkElement element)
{
    var canvas = element.Parent as Canvas;

    double left = Canvas.GetLeft(element);
    double top = Canvas.GetTop(element);

    bool isLeftAligned = !double.IsNaN(left);
    bool isTopAligned = !double.IsNaN(top);

    double x;
    double y;

    if (isLeftAligned)
    {
        x = left;
    }
    else
    {
        x = canvas.Width - Canvas.GetRight(element) - element.Width;
    }

    if (isTopAligned)
    {
        y = top;
    }
    else
    {
        y = canvas.Height - Canvas.GetBottom(element) - element.Height;
    }

    return new Point(x, y);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文