【问题标题】:How to increase font size in XAML?如何增加 XAML 中的字体大小?
【发布时间】:2012-08-11 13:18:25
【问题描述】:

如何增加文本块的字体?我想要这样的东西:

<TextBlock FontSize="20">
  text
</TextBlock>

因为当用户更改 Windows 的控件字体大小设置时,它将无法正常工作。我们是否有类似+VALUE(例如+2)的东西,类似于HTML?

编辑
这就是我所说的 Windows 设置的意思:

但我收到的答案完全让我满意。

【问题讨论】:

  • “当用户更改 Windows 的控件字体大小设置时它无法正常工作”是什么意思?我不认为用户可以设置单个控件类型的大小(在“高级外观设置”对话框中没有任何内容)。他们可以将所有屏幕内容缩放到 125% 或 150%,但 WPF 会尊重这一点。所以我不确定你认为 WPF 会忽略什么设置。
  • 我进行了编辑以回答您的问题。

标签: c# xaml font-size


【解决方案1】:

WPF 没有 em 字体大小,在这个SO的答案中有替代品

最简单的可能是

<ScaleTransform ScaleX="1.2" ScaleY="1.2" /> 

【讨论】:

    【解决方案2】:

    将 Bob Vale 的答案改编为您的原始代码

    <TextBlock>
        <TextBlock.RenderTransform>
            <ScaleTransform ScaleX="1.2" ScaleY="1.2" />
        </TextBlock.RenderTransform>
        text
    </TextBlock>
    

    【讨论】:

      【解决方案3】:

      首先,您应该为您的字体大小创建一个应用程序范围的样式,如本答案所述:WPF global font size

      然后,您可以将 fontsize 值绑定到采用控制面板中定义的大小的静态类的属性,并相应地对其进行调整。

      【讨论】:

        【解决方案4】:

        对于那些发现这个问题需要相对字体大小机制来实现设计目的的可怜人,例如您在 css 中使用的,我发现了一个似乎在 WPF 中工作的 hack。

        这样用的:

        <StackPanel>
            <TextBlock>outer</TextBlock>
            <ContentControl local:RelativeFontSize.RelativeFontSize="2">
                <StackPanel>
                    <TextBlock>inner</TextBlock>
                    <ContentControl local:RelativeFontSize.RelativeFontSize="2">
                        <StackPanel>
                            <TextBlock>innerest</TextBlock>
                        </StackPanel>
                    </ContentControl>
                </StackPanel>
            </ContentControl>
        </StackPanel>
        

        这给出了这个:

        这是代码:

        public static class RelativeFontSize
        {
            public static readonly DependencyProperty RelativeFontSizeProperty =
                DependencyProperty.RegisterAttached("RelativeFontSize", typeof(Double), typeof(RelativeFontSize), new PropertyMetadata(1.0, HandlePropertyChanged));
        
            public static Double GetRelativeFontSize(Control target)
                => (Double)target.GetValue(RelativeFontSizeProperty);
        
            public static void SetRelativeFontSize(Control target, Double value)
                => target.SetValue(RelativeFontSizeProperty, value);
        
            static Boolean isInTrickery = false;
        
            public static void HandlePropertyChanged(Object target, DependencyPropertyChangedEventArgs args)
            {
                if (isInTrickery) return;
        
                if (target is Control control)
                {
                    isInTrickery = true;
        
                    try
                    {
                        control.SetValue(Control.FontSizeProperty, DependencyProperty.UnsetValue);
        
                        var unchangedFontSize = control.FontSize;
        
                        var value = (Double)args.NewValue;
        
                        control.FontSize = unchangedFontSize * value;
        
                        control.SetValue(Control.FontSizeProperty, unchangedFontSize * value);
                    }
                    finally
                    {
                        isInTrickery = false;
                    }
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2013-04-03
          • 2015-12-05
          • 1970-01-01
          • 2017-10-07
          • 2014-09-22
          • 2012-11-07
          • 1970-01-01
          • 2014-12-15
          • 2010-10-10
          相关资源
          最近更新 更多