Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding to X Y coordinates of element on WPF Canvas

Tags:

c#

.net

wpf

xaml

I have a Canvas with 2 "dots" drawn on it. 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.

Next, I want to put a TextBlock near to each Ellipse (a label). However, I don't want to include this TextBlock into the Canvas because it will then rotate also. I want the text to remain horizontal.

Any idea how to solve this in an elegant way?

like image 471
Robbert Dam Avatar asked Jan 04 '12 15:01

Robbert Dam


1 Answers

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.

like image 153
Tigran Avatar answered Nov 16 '22 05:11

Tigran