WPF/XAML:如何使 TextBlock 中的所有文本大写?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24956046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:06:55  来源:igfitidea点击:

WPF/XAML: How to make all text upper case in TextBlock?

wpfxamlbindingtextblockuppercase

提问by feralbino

I want all characters in a TextBlock to be displayed in uppercase

我希望 TextBlock 中的所有字符都以大写显示

 <TextBlock Name="tbAbc"
            FontSize="12"
            TextAlignment="Center"
            Text="Channel Name"
            Foreground="{DynamicResource {x:Static r:RibbonSkinResources.RibbonGroupLabelFontColorBrushKey}}" />

The strings are taken through Binding. I don't want to make the strings uppercase in the dictionary itself.

字符串是通过 Binding 获取的。我不想在字典本身中使字符串大写。

回答by TheEye

Or use

或使用

Typography.Capitals="AllSmallCaps"

in your TextBlockdefinition.

在你的TextBlock定义中。

See here: MSDN - Typography.Capitals

请参阅此处:MSDN - Typography.Capitals

EDIT:

编辑:

This does not work in Windows Phone 8.1, only in Windows 8.1 ...

这不适用于Windows Phone 8.1,仅适用于 Windows 8.1 ...

回答by kidshaw

Implement a custom converter.

实现自定义转换器。

using System.Globalization;
using System.Windows.Data;
// ...
public class StringToUpperConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null && value is string )
        {
            return ((string)value).ToUpper();
        }

        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

Then include that in your XAML as a resource:

然后将其作为资源包含在您的 XAML 中:

<local:StringToUpperConverter  x:Key="StringToUpperConverter"/>

And add it to your binding:

并将其添加到您的绑定中:

Converter={StaticResource StringToUpperConverter}

回答by Johan Larsson

You can use an attached property like this:

您可以使用这样的附加属性:

public static class TextBlock
{
    public static readonly DependencyProperty CharacterCasingProperty = DependencyProperty.RegisterAttached(
        "CharacterCasing",
        typeof(CharacterCasing),
        typeof(TextBlock),
        new FrameworkPropertyMetadata(
            CharacterCasing.Normal,
            FrameworkPropertyMetadataOptions.Inherits | FrameworkPropertyMetadataOptions.NotDataBindable,
            OnCharacterCasingChanged));

    private static readonly DependencyProperty TextProxyProperty = DependencyProperty.RegisterAttached(
        "TextProxy",
        typeof(string),
        typeof(TextBlock),
        new PropertyMetadata(default(string), OnTextProxyChanged));

    private static readonly PropertyPath TextPropertyPath = new PropertyPath("Text");


    public static void SetCharacterCasing(DependencyObject element, CharacterCasing value)
    {
        element.SetValue(CharacterCasingProperty, value);
    }

    public static CharacterCasing GetCharacterCasing(DependencyObject element)
    {
        return (CharacterCasing)element.GetValue(CharacterCasingProperty);
    }

    private static void OnCharacterCasingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is System.Windows.Controls.TextBlock textBlock)
        {
            if (BindingOperations.GetBinding(textBlock, TextProxyProperty) == null)
            {
                BindingOperations.SetBinding(
                    textBlock,
                    TextProxyProperty,
                    new Binding
                    {
                        Path = TextPropertyPath,
                        RelativeSource = RelativeSource.Self,
                        Mode = BindingMode.OneWay,
                    });
            }
        }
    }

    private static void OnTextProxyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        d.SetCurrentValue(System.Windows.Controls.TextBlock.TextProperty, Format((string)e.NewValue, GetCharacterCasing(d)));

        string Format(string text, CharacterCasing casing)
        {
            if (string.IsNullOrEmpty(text))
            {
                return text;
            }

            switch (casing)
            {
                case CharacterCasing.Normal:
                    return text;
                case CharacterCasing.Lower:
                    return text.ToLower();
                case CharacterCasing.Upper:
                    return text.ToUpper();
                default:
                    throw new ArgumentOutOfRangeException(nameof(casing), casing, null);
            }
        }
    }
}

Then usage in xaml will look like:

然后在 xaml 中的用法将如下所示:

<StackPanel>
    <TextBox x:Name="TextBox" Text="abc" />
    <TextBlock local:TextBlock.CharacterCasing="Upper" Text="abc" />
    <TextBlock local:TextBlock.CharacterCasing="Upper" Text="{Binding ElementName=TextBox, Path=Text}" />
    <Button local:TextBlock.CharacterCasing="Upper" Content="abc" />
    <Button local:TextBlock.CharacterCasing="Upper" Content="{Binding ElementName=TextBox, Path=Text}" />
</StackPanel>

回答by RazvanR

If it's not a big deal you could use TextBoxinstead of TextBlocklike this:

如果这不是什么大问题,您可以像这样使用TextBox而不是TextBlock

<TextBox CharacterCasing="Upper" IsReadOnly="True" />

回答by Stephen Drew

I use a character casing value converter:

我使用字符大小写值转换器:

class CharacterCasingConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var s = value as string;
        if (s == null)
            return value;

        CharacterCasing casing;
        if (!Enum.TryParse(parameter as string, out casing))
            casing = CharacterCasing.Upper;

        switch (casing)
        {
            case CharacterCasing.Lower:
                return s.ToLower(culture);
            case CharacterCasing.Upper:
                return s.ToUpper(culture);
            default:
                return s;
        }
    }

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

回答by Marcel B

How about a Converterthat converts your text to uppercase. This way you original text stays unchanged.

将您的文本转换为大写的转换器怎么样。这样你的原始文本保持不变。

How to use IValueConverter in Binding of WPF

WPF绑定中如何使用IValueConverter