How can I (programatically) set the font size of a
TextBlock in a WPF
The text size should be relative to the canvas dimensions, so that it is centered.
This works for my local machine, but displayed on a larger screen, the text is cut off halfway up.
double fontSize = TickerOverlay.Height / 2.5;
Is there a simple way to do this, so that If the canvas dimensions increase (say, displayed on a larger resolution screen), the text will still be centered?
Best How To :
The correct way to do this in a model view controller layout is for this to be handled by the view (In this case XAML). WPF has a component, the ViewBox, that works well for this case.
<Viewbox HorizontalAlignment="Center" Grid.Row="1" VerticalAlignment="Center" Height="50">
<TextBlock Margin="10 0 10 0">Hello World</TextBlock>
The viewbox automatically expands to fill up all available space and sets the font to fit. Height is how you set the max font size, though it will shrink if it reaches 100%. Use the margin to ensure it doesn't go off the page.