0

Not sure if this is hard to do in XAML, but I have some TextBlock that I am showing on top of the main window using a fixed font size.

If this is larger than the main window, it resizes the main window. I don't want this. I tried this so far but couldn't prevent the main window from getting resized horizontally.

What I want is to resize the TextBlock if it's not going to fit the main window width. Main window width is dynamic so I can't use a fixed unit value.

Here is the code:

<Viewbox StretchDirection="DownOnly">
    <Grid Name="InfoTextOverlay" Visibility="Hidden" HorizontalAlignment="Center">
        <TextBlock Text="{Binding InfoText, ElementName=MyMainWindow}" Foreground="White" FontSize="40" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
</Viewbox>
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
  • Does this answer your question? [How to automatically scale font size for a group of controls?](https://stackoverflow.com/questions/15641473/how-to-automatically-scale-font-size-for-a-group-of-controls) – Eduards Aug 29 '21 at 18:57
  • With / without the `Viewbox` your code does not resizes the main window for me . Set `SizeToContent` property of the window to `Manual` , if its not. – Maria Aug 29 '21 at 19:40
  • @Maria, I need the main window to adapt to data grid control though, so it needs to always fit its contents, but not the textblock that's shown on top. – Joan Venge Aug 30 '21 at 01:06
  • @Squirrel.98: thx, I will check it today. – Joan Venge Aug 30 '21 at 01:06
  • @Squirrel.98: I just checked it but it seems overlay complicated. Not sure how to apply to my case, because he has multiple text, etc. Whereas I just want to use fixed font size of my choosing unless it doesn't fit my window, then resize to fit with some margin. – Joan Venge Aug 31 '21 at 06:51

1 Answers1

0

if you want to avoid auto width, use TextWrapping="Wrap".

or

Just remove the FontSize property if you need auto-resizing of the text block