1

I am trying to get the rendered location of the corners of a textblock. I can't seem to find out how to do it. The textblock is bound by a border, which in turn in bound by a grid. Therefore the textblock and the border have no dimensions that I can pull in relation to the rest of the Window. I tried the following code, but only part of it works. The margin is 0 and the ActualWidth is 13.2. By the time that this line has been reached, the window and textblocks have been rendered.

double rightBound = textBlock.Margin.Left + textBlock.ActualWidth;

Update: I have the following code which mostly does what I want. However, the points DO NOT line up with the UI elements well; they appear to be arbitrarily placed near the elements. The points do not line up with the boundaries of the block.

Point blockPoint = textBlock.TranslatePoint(new Point(0, 0), myMainWindow);
Sean
  • 336
  • 1
  • 3
  • 15
  • In which method is that line? has the window actually drawn at this point? – Andy Mar 22 '16 at 17:04
  • The window has already been rendered as well as the textblocks. – Sean Mar 22 '16 at 17:04
  • 2
    You can use the [TranslatePoint](https://msdn.microsoft.com/en-us/library/system.windows.uielement.translatepoint(v=vs.110).aspx) method to get the position relative to any other element. – Clemens Mar 22 '16 at 17:11
  • Could you please clarify how to use TranslatePoint? The documentation is unclear and my code can't seem to find the reference. – Sean Mar 22 '16 at 17:37
  • How do you know that they don't line up with the element? What are you doing with the points after you translate them? – o_weisman Mar 28 '16 at 13:09
  • I am literally not doing anything after I grab the point location with the code above. I used it the exact point x and y and plot them as an ellipse on my graph. – Sean Mar 28 '16 at 19:42
  • 1
    Your problem will be solved faster if you provide more details (screenshot?) on how you plot that point and what is the expected result. – Evk Mar 28 '16 at 20:17

1 Answers1

1

So your text box isn't dimensioned and is encapsulated by a border... The code you gave above might be grabbing the position of the text within the border, thus appearing like the lines are drawn randomly inside the element. Try grabbing the encapsulating border's position.

Ben
  • 153
  • 9
  • My guess is that because your textbox isn't sized at creation, every time that you change the text, the textbox and not the border resizes. This means that the point is somewhere inside the border (like you describe). – Ben Mar 29 '16 at 13:45