3

I have a TextBlock nested in a Viewbox.

<Viewbox x:Name="ContentPanel">
    <TextBlock x:Name="CharacterBlock" TextAlignment="Center" Text="愛" />
</Viewbox>

How can I know the height and width of the TextBlock? RenderSize and ActualWidth/Height all return its original size, instead of it's size as scaled by the Viewbox.

Clément
  • 12,299
  • 15
  • 75
  • 115
  • Is taking `Viewbox.ActualWidth/Height` an option? – LPL Jan 26 '13 at 10:45
  • @LPL no, because the Viewbox is larger than the Textblock (same widths, different heights, sicne the viewbox fills a parent grid). – Clément Jan 26 '13 at 15:17
  • @Clément I don't get what you mean. `Viewbox.ActualWidth` and `Viewbox.ActualHeight` return the stretched width and height of the Viewbox content. See the [Stretch](http://msdn.microsoft.com/en-us/library/system.windows.media.stretch.aspx) enum for how a Viewbox stretches its content. – Clemens Jan 26 '13 at 17:08
  • @Clemens: Suppose your Viewbox fills a 1000px*500px area, and contains a textblock whose initial size is 250px*250px. Uniform scaling will enlarge the TextBlock to 500px*500px, but Viewbox.ActualWidth/Height will still return 1000px and 500px. I'd like a way to retrieve the size of the textblock. – Clément Jan 26 '13 at 17:19
  • If I put your Viewbox (with your TextBlock as content) in a Grid sized 1000x500, the Viewbox size is reported as ActualWidth=376, ActualHeight=500. – Clemens Jan 26 '13 at 17:24
  • See answer [here](https://stackoverflow.com/a/5371410/7206675) – codeDom Feb 26 '23 at 09:31

0 Answers0