11

Consider the following:

Let's say the Window is 1024x768 and the ViewBox fills the entire window, this means the TextBox is really large on the screen.

I want to get the size of the TextBox as it is currently on the screen. If I get DesiredSize or ActualSize or even RenderedSize I always get 100.

Any suggestions?

Update: I could probably get the ActualWidth of the ViewBox and divide it by the ActualWidth of it's child which would give me the current scale factor and expose that as a property somehow but I'm not sure that's the best way to do it.

Cœur
  • 37,241
  • 25
  • 195
  • 267
TimothyP
  • 21,178
  • 26
  • 94
  • 142
  • Have you considered checking the "Width" and "Height" properties of the TextBox? – CodeMouse92 Feb 22 '11 at 18:59
  • 1
    @JasonMc92 - That would really won't help, as Width & Height would be most probably null. Width and Height are used to *request* sizes, not to ask for the actual size. – Elad Katz Mar 09 '11 at 21:58

1 Answers1

23

This is how you get the ScaleTransform the ViewBox exerts on its children:

var child = VisualTreeHelper.GetChild(viewBox, 0) as ContainerVisual;
var scale = child.Transform as ScaleTransform;

Here viewBox is the ViewBox that textbox sits in. Then you can just multiply scale.ScaleX * textBox.ActualWidth and you get the size in Screen coordinates

But it gets even easier! To get that textbox's size directly in Screen Coordinates you do:

textbox.PointToScreen(new Point(textbox.ActualWidth,textbox.ActualHeight)) - textbox.PointToScreen(new Point(0,0))
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Markus Hütter
  • 7,796
  • 1
  • 36
  • 63