Gnyasha
Gnyasha

Reputation: 684

WPF Calculated FontSize Inside ViewBox

I have faced a problem and tried the internet and threads here to find a solution but haven't found yet. I am a beginner in programming any help will be appreciated.

I have a window which will be displayed on the secondary screen and a TextBlock wrapped in a viewbox and the text is binded and will be populated dymnamically on runtime on the second screen. The fontsize is also Binded and can be changed by the user.

This is working perfectly well. The Problem Is That when the user increases fontsize to a higher value the text looks good if the text is short but if the text is very very large the text looks ugly sometimes becomes unreadable.

I have tried the ViewBox's Sretch direction and stretch but haven't found a better way to solve my problem but manually if there is very very long text and the text is unreadable reducing the fontsize makes text readable.

How may i be helped. One solution I have in mind is to calculate the fontsize maybe on height and width of parent grid such that when the text is very long the calculation reduces the fontsize to where it becomes readable.

here is my sample Xaml

<Grid>
    <Viewbox Stretch="Fill"
             StretchDirection="DownOnly">
        <TextBlock Text="{Binding}"
                   Width="{Binding}"
                   FontSize="{Binding}"
                   TextWrapping="Wrap">

        </TextBlock>
    </Viewbox>
</Grid>

Here is the Ugly Behavior when text is very long and fontsize is set highly

Expected behavior. Here the fontsize is manually set lower

Upvotes: 0

Views: 401

Answers (1)

Gnyasha
Gnyasha

Reputation: 684

Okay I found a work around the problem. If anyone has another solution it will be welcomed.

I wrapped the textblock inside a border and removed binding of the width of the textblock and I will be increasing and decreasing the width. If font size increases I will decrease the width by a ratio in the ViewModel and vice versa.

<Grid>
<Viewbox >
    <Border>
    <TextBlock Text="{Binding}"
               Width="{Binding}"
               FontSize="{Binding}"
               TextWrapping="Wrap">

    </TextBlock>
   </Border>
</Viewbox>

Upvotes: 1

Related Questions