Hal*_*rim 7 wpf xaml resize font-size viewbox
我有Viewbox一些TextBlocks被缩放和定位完美的s ViewBox.像这样的东西:
<Viewbox Stretch="Uniform">
<Canvas Width="100" Height="100">
<Ellipse Width="100" Height="100" Stroke="Black"/>
<TextBlock Width="100" TextAlignment="Center" FontSize="12">Top Center</TextBlock>
</Canvas>
</Viewbox>
Run Code Online (Sandbox Code Playgroud)
如果用户调整大小,Viewbox其内容将完美缩放以匹配.但是我想保持FontSize12,不管实际的大小Viewbox.
我怎样才能做到这一点?我可以在XAML中执行此操作而不附加Resize事件吗?
Avi*_* P. 11
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>
Run Code Online (Sandbox Code Playgroud)
请注意,我从中移除了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();
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
<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>
Run Code Online (Sandbox Code Playgroud)
这也许是一个简单的解决方案.
<Viewbox StretchDirection="DownOnly" >
<Label Content="Enable" FontSize="10" FontStretch="Normal" />
</Viewbox>
Run Code Online (Sandbox Code Playgroud)