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

StackOverflow https://stackoverflow.com/questions/19436368

  •  01-07-2022
  •  | 
  •  

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

有帮助吗?

解决方案

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

其他提示

Would something like this be suitable?

<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 StretchDirection="Both" Stretch="Uniform">
            <TextBox Text="some text"></TextBox>
        </Viewbox>
    </Grid>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top