获取 WPF 中 InkCanvas 内元素的 XY 坐标
我有一个 InkCanvas,它的子级是通过 C# 以编程方式添加的。它们不是通过 InkCanvas.LeftProperty
或 InkCanvas.TopProperty
设置的。我想知道是否有可能获得元素的默认 X 和 Y 值。目前,如果我检查 InkCanvas.LeftProperty
或 InkCanvas.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这可能是答案:
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.
您想要使用 Canvas 上定义的各种静态方法来获取这些值 - GetLeft(), GetTop() 等。我认为
LeftProperty
只是附加属性本身的定义,而不是获取值的方式。You want to use various static methods defined on
Canvas
to get those values - GetLeft(), GetTop(), etc. I believe thatLeftProperty
is just the definition of the attached property itself, not the way to get the value.