ViewBox
一定のフォントサイズを維持することはできません。それが機能する方法ではありません。そのためには、ビュー ボックスの外にテキストを配置する必要があります。
<Grid>
<Viewbox Stretch="Uniform">
<Canvas Width="100" Height="100">
<Ellipse Width="100" Height="100" Stroke="Black"/>
</Canvas>
</Viewbox>
<TextBlock TextAlignment="Center" FontSize="12">Top Center</TextBlock>
</Grid>
から Width プロパティを削除したことに注意してくださいTextBlock
。テキストの配置で中央揃えを処理できるように、グリッドの幅に合わせて伸ばすだけです。
または、創造性を発揮してFontSize
プロパティを にActualWidth
バインドし、ViewBox
適切にスケーリングすることもできます。次に例を示します。
コンバータ:
class ViewBoxConstantFontSizeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is double)) return null;
double d = (double)value;
return 100 / d * 12;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
使用法:
<Window.Resources>
...
<local:ViewBoxConstantFontSizeConverter x:Key="conv"/>
</Window.Resources>
...
<Viewbox Name="vb" Stretch="Uniform">
<Canvas Width="100" Height="100">
<Ellipse Width="100" Height="100" Stroke="Black"/>
<TextBlock Width="100" TextAlignment="Center"
FontSize="{Binding ElementName=vb,
Path=ActualWidth,
Converter={StaticResource conv}}">
Top Center
</TextBlock>
</Canvas>
</Viewbox>