简体   繁体   English

绑定到WPF画布上元素的XY坐标

[英]Binding to X Y coordinates of element on WPF Canvas

I have a Canvas with 2 "dots" drawn on it. 我的画布上有2个“点”。 See this (simplified) code: 参见以下(简化)代码:

<Canvas> 
    <Ellipse />
    <Ellipse />
    <Canvas.RenderTransform>
        <RotateTransform x:Name="rotateEllipse" />
    </Canvas.RenderTransform>
</Canvas>

As you can see, I want to rotate the canvas using the given RotateTransform. 如您所见,我想使用给定的RotateTransform旋转画布。

Next, I want to put a TextBlock near to each Ellipse (a label). 接下来,我想在每个Ellipse(标签)附近放置一个TextBlock。 However, I don't want to include this TextBlock into the Canvas because it will then rotate also. 但是,我不想将此TextBlock包含在Canvas中,因为它随后也会旋转。 I want the text to remain horizontal. 我希望文本保持水平。

Any idea how to solve this in an elegant way? 知道如何以一种优雅的方式解决这个问题吗?

Something like this, should work for you 这样的东西应该为您工作

<TextBlock RenderTransform="{Binding RelativeSource={RelativeSource AncestorType=Canvas},
                                                      Path=RenderTransform.Inverse}"/>

Assign to text box transformation matrix an inverse of the transformation matrix of the Canvas . 向文本框transformation matrix分配Canvas transformation matrix矩阵。

Good question! 好问题! And I'm going to guess, so please take this answer with a pinch of salt. 我会猜测,所以请用少许盐来回答这个问题。

I believe you are trying to place text annotations next to ellipses on a rotated canvas, but these annotations need to remain horizontal. 我相信您正在尝试在旋转的画布上的椭圆旁边放置文本注释,但是这些注释需要保持水平。 Two things you could try: 您可以尝试两件事:

  • Firstly, given the XY point that you know of each ellipse from Canvas.GetTop/GetLeft, you could find its new rotated XY location by applying the RotateTransform to the ellipse location, using the formula U = M*V, where U is the output point, V is the input point (XY location of ellipse) and M is the Rotation Matrix. 首先,给定Canvas.GetTop / GetLeft中每个椭圆的XY点,您可以使用公式U = M * V将RotateTransform应用于椭圆位置,从而找到其新的旋转XY位置,其中U为输出点,V是输入点(椭圆的XY位置),M是旋转矩阵。

  • Secondly, you could place a second canvas over the first (assuming they are both in a grid, the second canvas is at higher Z-index and is the same size as the underlying canvas). 其次,您可以在第二个画布上放置第二个画布(假设它们都在网格中,第二个画布的Z索引较高,并且与基础画布的大小相同)。 Call it an annotation layer. 将其称为注释层。 Your annotations (text labels) can appear at the new transformed locations and unrotated using this approach. 您的注释(文本标签)可以出现在新的转换位置,并且可以使用此方法旋转。

You'd have to do this in code of course, not Xaml, although you might find a binding friendly approach by creating a value converter on the TextBlock that bound to the source RotateTransform/Ellipse and did this operation for you. 尽管您可能会通过在TextBlock上创建一个绑定到源RotateTransform / Ellipse的值转换器上创建一个值转换器找到绑定友好的方法,但是您必须使用代码(而不是Xaml)来执行此操作,但是您可能需要这样做。

Another approach would be to take the .Inverse of the RotateTransform and apply that to the textblocks, however you may still need to translate to get to the new location. 另一种方法是采用RotateTransform的.Inverse并将其应用于文本块,但是您可能仍需要转换才能到达新位置。 Either way I think it demands some experimentation. 无论哪种方式,我认为都需要进行一些实验。

Best regards, 最好的祝福,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM