我在网格中的WPF中有一些TextBlocks,我想根据它们的可用宽度/高度进行缩放.当我搜索自动缩放字体大小时,典型的建议是将TextBlock放入ViewBox.
所以我这样做了:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Viewbox MaxHeight="18" Grid.Column="0" Stretch="Uniform" Margin="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBlock Text="{Binding Text1}" />
</Viewbox>
<Viewbox MaxHeight="18" Grid.Column="1" Stretch="Uniform" Margin="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBlock Text="{Binding Text2}" />
</Viewbox>
<Viewbox MaxHeight="18" Grid.Column="2" Stretch="Uniform" Margin="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBlock Text="{Binding Text3}" />
</Viewbox>
</Grid>
Run Code Online (Sandbox Code Playgroud)
它会自动缩放每个TextBlock的字体.但是,这看起来很有趣,因为如果其中一个TextBlocks具有较长的文本,那么它将使用较小的字体,而它的相邻网格元素将使用较大的字体.我希望字体大小按组扩展,如果我可以为一组控件指定"SharedSizeGroup"以自动调整其字体大小,那也许会很好.
例如
第一个文本块文本可能是"3/26/2013 10:45:30 AM",第二个TextBlocks文本可能是"FileName.ext".如果它们跨越窗口的宽度,则用户开始调整窗口的大小越来越小.日期将开始使其字体小于文件名,具体取决于文件名的长度.
理想情况下,一旦其中一个文本字段开始调整字体磅值大小,它们就会匹配.有没有人想出一个解决方案,或者可以让我了解你如何使它工作?如果它需要自定义代码,那么希望我们/我可以将其重新打包为自定义混合或附加行为,以便将来可以重复使用.我认为这是一个相当普遍的问题,但我无法通过搜索找到任何内容.
更新 我尝试了Mathieu的建议,但它有一些副作用:
<Window x:Class="WpfApplication6.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="270" Width="522">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Rectangle Grid.Row="0" Fill="SkyBlue" />
<Viewbox Grid.Row="1" …Run Code Online (Sandbox Code Playgroud)