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

GG.*_*GG. 35 wpf formatting xaml textblock

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

<TextBlock Text="{StaticResource String1}"/>
<MenuItem Header="{StaticResource MenuItemDoThisAndThat}"/>
Run Code Online (Sandbox Code Playgroud)

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

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

Pet*_*ter 35

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

<TextBlock Text="{Binding Source={StaticResource String1},  Converter ={StaticResource myConverter}}"/>
Run Code Online (Sandbox Code Playgroud)

  • 当你想将这个大写转换器添加到Style时的任何解决方案?如果您在Setter中执行此操作,如果您稍后尝试在XAML中绑定到Text属性,它将被覆盖.似乎除了为每个实例添加此转换器之外别无选择. (7认同)

scr*_*789 30

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

<TextBox CharacterCasing="Upper" Text="{StaticResource String1}" />
Run Code Online (Sandbox Code Playgroud)

  • TextBlock没有CharacterCasing属性 - 但TextBox确实如此. (43认同)
  • 另一个"同行评审"的例子出了问题.28票?它不仅******关于**TextBlock**的OP问题,但正如vaughan指出的那样,**不会工作** (21认同)
  • 正如文档所指出的那样,CharacterCasing只会影响输入的字母,因此甚至无法实现对TextBox的预期效果 (7认同)
  • 这有22个赞成票.正如先前的评论所述,这与OP无关. (2认同)

Ali*_*ese 25

我认为这对你有用

<TextBlock Text='{StaticResource String1}' Typography.Capitals="AllSmallCaps"/>
Run Code Online (Sandbox Code Playgroud)

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

  • 这会生成小于字体大小的文本. (9认同)
  • 我还应该提到"AllPetiteCaps"不适用于所有字体:http://stackoverflow.com/a/638871/2758833 (4认同)
  • `&lt;TextBlock Text='{StaticResource String1}' Typography.Capitals="AllPetiteCaps"/&gt;` 这将使它与文本的大小相同 (2认同)

Max*_*Max 12

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

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

XAML:

<TextBlock Text="{Binding Source={StaticResource String1}, Converter ={StaticResource myCaseConverter}}"/>
Run Code Online (Sandbox Code Playgroud)

  • 是的,拥有更多功能的转换器.您可以在XAML中更改case属性:<local:CaseConverter Case ="Lower"x:Key ="CaseConverter"/> (3认同)

小智 7

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

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

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

这是附属物:

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 你可以把它放在一个样式中,并且可能适用于所有文本框. (2认同)