wpf 如何使所有文本大写/大写?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1762485/
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-08 21:14:03  来源:igfitidea点击:

How to make all text upper case / capital?

wpfxamlformattingtextblock

提问by GG.

I want all texts in TextBlock, Label, MenuItem.Headerto be displayed in upper case. The strings are taken from a ResourceDictionarye.g.:

我想在所有文本TextBlockLabelMenuItem.Header以大写形式显示。字符串取自ResourceDictionary例如:

<TextBlock Text="{StaticResource String1}"/>
<MenuItem Header="{StaticResource MenuItemDoThisAndThat}"/>

etc. (also for Labeland other controls)

等(也用于Label和其他控件)

I cannot use a value converter because there is no binding. I don't want to make the strings upper case in the dictionary itself.

我不能使用值转换器,因为没有绑定。我不想在字典本身中使字符串大写。

采纳答案by Peter

You still can use a converter, just set the textvalue in the source of the binding :

您仍然可以使用转换器,只需在绑定源中设置文本值:

<TextBlock Text="{Binding Source={StaticResource String1},  Converter ={StaticResource myConverter}}"/>

回答by scrat789

Rather than using a converter, you can use the tag CharacterCasing in a TextBox but in your case, it doesn't work on a TextBlock.

您可以在 TextBox 中使用标签 CharacterCasing 而不是使用转换器,但在您的情况下,它不适用于 TextBlock。

<TextBox CharacterCasing="Upper" Text="{StaticResource String1}" />

回答by Alias Varghese

I think this will work for you

我认为这对你有用

<TextBlock Text='{StaticResource String1}' Typography.Capitals="AllSmallCaps"/>

For font capitals enumerations https://msdn.microsoft.com/en-us/library/system.windows.fontcapitals(v=vs.110).aspx

对于字体大写枚举https://msdn.microsoft.com/en-us/library/system.windows.fontcapitals(v=vs.110).aspx

回答by Max

To complete Peter's answer (my edit has been rejected), you can use a converter like this:

要完成彼得的回答(我的编辑被拒绝),您可以使用这样的转换器:

C#:

C#:

public class CaseConverter : IValueConverter
{    
    public CharacterCasing Case { get; set; }

    public CaseConverter()
    {
        Case = CharacterCasing.Upper;
    }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var str = value as string;
        if (str != null)
        {
            switch (Case)
            {
                case CharacterCasing.Lower:
                    return str.ToLower();
                case CharacterCasing.Normal:
                    return str;
                case CharacterCasing.Upper:
                    return str.ToUpper();
                default:
                    return str;
            }
        }
        return string.Empty;
    }

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

XAML:

XAML:

<TextBlock Text="{Binding Source={StaticResource String1}, Converter ={StaticResource myCaseConverter}}"/>

回答by Lauren

I created an attached property and converter for this. You probably already have the converter, so replace my reference to CaseConverter to whatever implementation you have.

我为此创建了一个附加属性和转换器。您可能已经有了转换器,因此请将我对 CaseConverter 的引用替换为您拥有的任何实现。

The attached property is just a boolean that you set if you want it to be uppercase (you could obviously extend this to instead be an enumerable for a selection of styles). When the property changes, it rebinds the TextBlock's Text property as needed, adding in the converter.

附加属性只是一个布尔值,如果您希望它为大写,则可以设置它(您显然可以将其扩展为可枚举的样式选择)。当属性更改时,它会根据需要重新绑定 TextBlock 的 Text 属性,并添加到转换器中。

A little more work might need to be done when the property is already bound - my solution assumes it's a simple Path binding. But it may need to also duplicate the source, etc. However I felt this example is enough to get my point across.

当属性已经绑定时,可能需要做更多的工作 - 我的解决方案假设它是一个简单的路径绑定。但它可能还需要复制源等。不过我觉得这个例子足以说明我的观点。

Here's the attached property:

这是附加的财产:

public static bool GetUppercase(DependencyObject obj)
    {
        return (bool)obj.GetValue(UppercaseProperty);
    }

    public static void SetUppercase(DependencyObject obj, bool value)
    {
        obj.SetValue(UppercaseProperty, value);
    }

    // Using a DependencyProperty as the backing store for Uppercase.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty UppercaseProperty =
        DependencyProperty.RegisterAttached("Uppercase", typeof(bool), typeof(TextHelper), new PropertyMetadata(false, OnUppercaseChanged));

    private static void OnUppercaseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TextBlock txt = d as TextBlock;

        if (txt == null) return;

        var val = (bool)e.NewValue;

        if (val)
        {
            // rebind the text using converter
            // if already bound, use it as source

            var original = txt.GetBindingExpression(TextBlock.TextProperty);

            var b = new Binding();

            if (original != null)
            {
                b.Path = original.ParentBinding.Path;
            }
            else
            {
                b.Source = txt.Text;
            }

            b.Converter = new CaseConverter() { Case = CharacterCasing.Upper };


            txt.SetBinding(TextBlock.TextProperty, b);
        }
    }

回答by Ahe

This does not strictly answer the question but does provide a trick to cause the same effect.

这并没有严格回答这个问题,但确实提供了一个导致相同效果的技巧。

I believe many finding their way here are looking how to do this with a style. TextBlock is a bit tricky here because it is not a Control but a FrameworkElement and therefore you can not define a Template to do the trick.

我相信很多人在这里找到了自己的方式,正在寻找如何以一种风格来做到这一点。TextBlock 在这里有点棘手,因为它不是 Control 而是 FrameworkElement,因此您无法定义模板来实现这一点。

The need to use all uppercase text is most likely for headings or something like that where use of Label is justified. My solution was:

对于标题或类似使用 Label 的内容,最有可能需要使用全部大写文本。我的解决方案是:

<!-- Examples of CaseConverter can be found in other answers -->

<ControlTemplate x:Key="UppercaseLabelTemplate" TargetType="{x:Type Label}">
    <TextBlock Text="{TemplateBinding Content, Converter={StaticResource CaseConverter}}" />
</ControlTemplate>

<Style x:Key="UppercaseHeadingStyle"
       TargetType="{x:Type Label}">
    <Setter Property="FontSize" Value="20" />
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="Template" Value="{StaticResource UppercaseLabelTemplate}" />
</Style>

<!-- Usage: -->
<Label Content="Header" Style="{StaticResource UppercaseHeadingStyle}" />

Note that this does disable some of the default behavior of Label, and works only for text, so I would not define this as default (no one probably wants all labels uppercase anyway). And of course you must use Label instead of TextBlock when you need this style. Also I would not use this inside of other templates, but only strictly as a topic style.

请注意,这确实禁用了 Label 的一些默认行为,并且仅适用于文本,因此我不会将其定义为默认值(反正没有人可能希望所有标签都大写)。当然,当您需要这种样式时,您必须使用 Label 而不是 TextBlock。此外,我不会在其他模板中使用它,而只会严格用作主题样式。

回答by Pascalsz

You can case all input into TextBox controls with the following property:

您可以使用以下属性将所有输入输入到 TextBox 控件中:

<TextBox CharacterCasing="Upper"

To apply to all TextBox controls in the entire application create a style for all TextBox controls:

要应用于整个应用程序中的所有 TextBox 控件,请为所有 TextBox 控件创建一个样式:

<Style TargetType="{x:Type TextBox}">
  <Setter Property="CharacterCasing" Value="Upper"/>
</Style>