画布内的 TranslatePoint
我的应用程序中有一个滚动查看器,其中包含一个画布,其中有一个自定义绘制的树结构。我试图获取画布中特定节点元素相对于滚动查看器的位置(以便我可以滚动到它),但我的尝试不起作用。
我尝试过使用 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.GetLeft
、Canvas.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过使用
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...我为这个问题编写了一个简单的解决方法。它有效,但不是很干净,所以如果有人对此有答案,我仍然会很感激!这是我当前使用的代码:
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: