尝试使用 C# 和 WPF 在文本框中调整 TextBox + 字体大小,只能做一个或另一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19436368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to resize TextBox + font size in text box with C# and WPF, can only do one or the other
提问by sdouble
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:


调整文本框大小有效,但我无法调整里面的文本大小:


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:


或者调整字体大小有效,但我无法让文本框填充我正在使用的视图框:


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)

使用 Stretch="Fill"(在正确的轨道上,但我宁愿保持统一,而 UniformToFill 做了一些奇怪的事情,我什至看不到发生了什么)

回答by kmatyaszek
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:
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:
结果:




回答by Animus Miles-Militis
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>

