Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

My UI requirement calls for multiple TextBlocks to scale within a Window. Not a problem, wrap them in a Viewbox - works great.

Here's my problem; The requirement states the font size must be the same in all TextBlocks. What that means to me is the largest font needs to scale down to the smallest font.

The following XAML file illustrates my issue. "Hello World" will be a much shorter size than "Hello" if I resize the Window to be narrow and tall.

Any ideas on how to achieve this?

I have a List<> of all Viewboxes, max is 8. I can set MaxHeight on a SizeChanged event on all of them, but I get stuck there.

Any help would be greatly appreciated.

Thanks in advance,

Jason


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="400" Width="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Viewbox x:Name="V1" Grid.Column="0" Grid.Row="0">
            <TextBlock x:Name="T1">Hello World</TextBlock>
        </Viewbox>
        <Viewbox x:Name="V2" Grid.Column="0" Grid.Row="1">
            <TextBlock x:Name="L2">Hello</TextBlock>
        </Viewbox>
    </Grid>
</Window>
Posted

1 solution

Hi Jason,

you don't need events nor additional sizing properties; just put only the Grid in a Viewbox and insert all TextBlocks with the same FontSize. This way you can also use multiline texts.

For your example, you might have to add some alignment properties to the TextBlocks, to resemble the ViewBox's standard behaviour, if that is needed.

Cheers
Jürgen
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900