4

たとえば、TextBlock のフォントを増やすにはどうすればよいですか。私はこのようなものを持ちたくありません:

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

ユーザーがコントロールのフォント サイズの Windows の設定を変更すると、正しく動作しないためです。HTML に似た+VALUE(例: ) のようなものはありますか?+2

編集
それが、Windowsの設定について話していることです。 ここに画像の説明を入力

しかし、私が受け取った答えは私を完全に満足させます.

4

4 に答える 4

10

WPFにはemフォントサイズがありません。このSOへの回答には代替案があります

シンプリストは

<ScaleTransform ScaleX="1.2" ScaleY="1.2" /> 
于 2012-08-15T08:24:27.043 に答える
7

Bob Vale の回答を元のコードに適応させる

<TextBlock>
    <TextBlock.RenderTransform>
        <ScaleTransform ScaleX="1.2" ScaleY="1.2" />
    </TextBlock.RenderTransform>
    text
</TextBlock>
于 2015-11-11T13:30:23.523 に答える
2

まず、この回答で説明されているように、フォント サイズに合わせてアプリケーション スコープのスタイルを作成する必要があります: WPF グローバル フォント サイズ

次に、フォントサイズの値を静的クラスのプロパティにバインドし、コントロール パネルで定義されたサイズを取得して、それに応じて適応させることができます。

于 2012-08-15T08:29:22.463 に答える
1

For those poor souls who find this questions in need for a relative font size mechanism for design purposes such as you'd use in css, I found a hack that appears to work in WPF.

It's used this way:

<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>

Which gives this:

screenshot

And here's the code:

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;
            }
        }
    }
}
于 2017-07-03T18:14:01.910 に答える