Trying to resize TextBox + font size in text box with C# and WPF, can only do one or the other

sdouble picture sdouble · Oct 17, 2013 · Viewed 7.4k times · Source

I'm trying to resize the textbox and the text inside of the textbox when the window is resized. I seem to be able to do one or the other, but not both at once.

Resizing the textbox works, but I can't resize the text inside: original size resized

Code for above example:

    <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="133*"/>
            <ColumnDefinition Width="20*"/>
            <ColumnDefinition Width="20*"/>
        </Grid.ColumnDefinitions>
        <Button Content="Button" Grid.Column="2"/>
        <Button Content="Button" Grid.Column="1"/>
        <TextBox TextWrapping="Wrap" VerticalContentAlignment="Center"/>
    </Grid>

Or resizing the font works, but I can't make the textbox fill the viewbox I'm using: original resized

Code for above example:

    <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="133*"/>
            <ColumnDefinition Width="20*"/>
            <ColumnDefinition Width="20*"/>
        </Grid.ColumnDefinitions>
        <Button Content="Button" Grid.Column="2"/>
        <Button Content="Button" Grid.Column="1"/>
        <Viewbox Stretch="Uniform">
            <TextBox TextWrapping="Wrap" VerticalContentAlignment="Center"/>
        </Viewbox>
    </Grid>

Using Stretch="Fill" (on the right track, but I'd rather keep it uniform, and UniformToFill does something weird that I can't even see what's going on) Fill

Answer

kmatyaszek picture kmatyaszek · Oct 17, 2013

You can use converter.

public class FontSizeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double actualHeight = System.Convert.ToDouble(value);
        int fontSize = (int)(actualHeight * .5);
        return fontSize;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML:

....
<Window.Resources>
    <local:FontSizeConverter x:Key="fontSizeCon" />
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="9*" />
        <RowDefinition Height="1*" />
    </Grid.RowDefinitions>     
    <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="133*"/>
            <ColumnDefinition Width="20*"/>
            <ColumnDefinition Width="20*"/>
        </Grid.ColumnDefinitions>
        <Button Content="Button" Grid.Column="2"/>
        <Button Content="Button" Grid.Column="1"/>
        <TextBox TextWrapping="Wrap" VerticalContentAlignment="Center"
                 FontSize="{Binding Path=ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}, Converter={StaticResource fontSizeCon}}"/>
    </Grid>      
</Grid>
...

Result:

enter image description here

enter image description here