获取 WPF 中 InkCanvas 内元素的 XY 坐标

发布于 2024-08-05 11:02:56 字数 304 浏览 8 评论 0原文

我有一个 InkCanvas,它的子级是通过 C# 以编程方式添加的。它们不是通过 InkCanvas.LeftPropertyInkCanvas.TopProperty 设置的。我想知道是否有可能获得元素的默认 X 和 Y 值。目前,如果我检查 InkCanvas.LeftPropertyInkCanvas.TopProperty ,它将返回 NaN,因为它们未设置。这有可能吗?预先感谢您的任何帮助。

干杯,

尼禄

I have an InkCanvas which has it's children programmically added in through C#. They are not set with InkCanvas.LeftProperty or InkCanvas.TopProperty. I was wondering if it was at all possible to get these default X and Y values of the elements. Currently if I check the InkCanvas.LeftProperty or InkCanvas.TopProperty it would return NaN as they are not set. Is this possible at all? Thanks in advance for any help.

Cheers,

Nilu

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

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

发布评论

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

评论(2

时光瘦了 2024-08-12 11:02:56

我认为这可能是答案:

Vector vector = VisualTreeHelper.GetOffset(element);

它返回一个向量,其元素相对于父级的偏移量。可以在此处找到更多信息。

编辑:不认为这会帮助我做我想做的事。尽管如果您最初在元素上设置边距,那么这是一个很好的方法。就我而言,我认为我必须明确设置坐标。

I think this might be the answer:

Vector vector = VisualTreeHelper.GetOffset(element);

It returns a vector with the element offset relative to the parent. More information can be found here.

Edit: Don't think this is going to help me for what I want to do. Although it's a good method if you are setting the margin on elements initially. In my case I think I would have to set the co-ordinates explicitly.

紅太極 2024-08-12 11:02:56

您想要使用 Canvas 上定义的各种静态方法来获取这些值 - GetLeft(), GetTop() 等。我认为 LeftProperty 只是附加属性本身的定义,而不是获取值的方式。

You want to use various static methods defined on Canvas to get those values - GetLeft(), GetTop(), etc. I believe that LeftProperty is just the definition of the attached property itself, not the way to get the value.

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