WPF:如何在Windows的FontSize更改时动态更新FontSize?

时间:2017-11-11 12:23:23

标签: c# wpf xaml binding attached-properties

我发现了错误,因此这里的代码对缩放文本感兴趣。

首先是ValueConverter和MarkupExtension,它是传递double作为参数所必需的。

public class ScaleValueConverter : IValueConverter
{
    public object Convert (object value, Type targetType, object parameter, CultureInfo culture)
    {
        double v = (double) value * (double) parameter;
        return v;
    }

    public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

public sealed class DoubleExtension : MarkupExtension
{
    public DoubleExtension (double value) { this.Value = value; }
    public double Value { get; set; }
    public override Object ProvideValue (IServiceProvider sp) { return Value; }
};

现在是XAML:

<TextBlock DockPanel.Dock="Top"
  FontSize="{Binding Path=FontSize, 
    RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window},
      Converter={StaticResource ScaleValueConverter},
  ConverterParameter={local:Int32 2}}"
>Some Text</TextBlock>

这样就可以通过调整Window的FontSize来缩放所有字体。所有其他字体大小都是相对的,取决于它。

0 个答案:

没有答案
相关问题