I have a panel with several TextBlock
s. I want to modify this panel such that the font size of each TextBlock changes depending on the size of the panel.
The size of the panel can change when the user changes the size of the window (e. g. maximizes or restores it).
I tried to do it using a Viewbox
:
<Viewbox Stretch="Fill">
<Grid Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Viewbox}}, Path=Width}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
<TextBlock
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
<TextBlock
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
<TextBlock
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
<TextBlock
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
<TextBlock
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
</Grid>
</Viewbox>
But it doesn't work because
Viewbox
increases andViewbox
decreases.How can implement scaling of these text blocks (so that they fill the entire available area, and adapt their font sizes) ?
You have height width auto and you are not placing the textblocks in the grid
<Grid>
<Viewbox Stretch="Fill">
<Grid Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Viewbox}}, Path=Width}">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="TextBlock1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
<TextBlock Grid.Column="1" Text="TextBlock2"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
<TextBlock Grid.Column="2" Text="TextBlock3"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
</Grid>
</Viewbox>
</Grid>