0

I have two controls on a form, they contain textblocks with text set from databinding, and they autosize to fill the available space.

What I want is for these two textblocks to use the same font size, so with two equally sized controls, if one has a longer string than the other, autosize means that the longer string will have a smaller font to fit. I want to set the other control to that font size so that they look visually consistent.

Is this acheivable?

For reference this is what I have, with databinding replaced with example strings:

<Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1">
    <Button.ContentTemplate>
        <DataTemplate>
            <Viewbox>
                <TextBlock Margin="10,10,10,10">Much Longer String</TextBlock>
            </Viewbox>
        </DataTemplate>
    </Button.ContentTemplate>
</Button>
<Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="1" Grid.Row="1">
    <Button.ContentTemplate>
        <DataTemplate>
            <Viewbox>
                <TextBlock Margin="10,10,10,10">Small</TextBlock>
            </Viewbox>
        </DataTemplate>
    </Button.ContentTemplate>
</Button>

Which then renders like this: Rendered result

Andy Wynn
  • 1,171
  • 7
  • 11
  • 1
    both button have the same FontSize. they are scaled differently. https://stackoverflow.com/questions/5423334/how-to-get-a-wpf-viewbox-coefficient-of-scaling-applied – ASh Apr 08 '22 at 08:20
  • If you remove the Viewbox, both TextBlock elements will be rendered equally in size. The Viewbox is scaling the content. Simply don't wrap the TextBlocks into a Viewbox. – BionicCode Apr 08 '22 at 13:37
  • @emoacht OK, so how do I do that? The two buttons are exactly the same dimensions. – Andy Wynn Apr 13 '22 at 06:47
  • @BionicCode That is correct, but somewhat misses the point of the question. I want the fonts to be the maximum size that the largest one can be, not a specified size. If I specify 10pt as the font size for both and they are both short strings, the form looks absurd with tiny strings in a sea of empty space. – Andy Wynn Apr 13 '22 at 06:50
  • @ASh Thanks for this, this does seem to be the answer I was looking for, I'll test this later. If you want to post this as an answer I'll mark is as correct if it works – Andy Wynn Apr 13 '22 at 06:53

0 Answers0