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

后端 未结 6 802
青春惊慌失措
青春惊慌失措 2021-02-13 17:59

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

 

        
6条回答
  •  南旧
    南旧 (楼主)
    2021-02-13 18:35

    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();
        }
    }
    

提交回复
热议问题