Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF scale text to fit only when too big

I am trying to setup a list of items in WPF which contains strings of random length (people's names). The majority of them are generally within a certain size, but occasionally you come across a string so long that it runs out of the bounds of it's container. I've normally just truncated it when it's too long, but I would much rather show the entirety of the string.

How can I force the text to remain it's normal size, unless too big to fit...in which case scale it down to fit?

NOTE: This is not the same as scaling all text to fit a certain size, which is accomplished using a viewbox around the text

IE: This is NOT what I want:

<Viewbox MaxWidth="100">
    <TextBlock Text="{Binding EmployeeDisplayName, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</Viewbox>

This makes everything scale up when too small, as well as scale down when too big. I ONLY want it to scale down when too big and NEVER scale up when too small...

Any thoughts?

like image 609
Robert Petz Avatar asked Aug 05 '13 23:08

Robert Petz


1 Answers

Use a Viewbox but set its StretchDirection property to DownOnly.

<Viewbox MaxWidth="100" StretchDirection="DownOnly">
    <TextBlock Text="{Binding EmployeeDisplayName, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</Viewbox>
like image 152
keyboardP Avatar answered Oct 05 '22 08:10

keyboardP