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

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

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

 

        
6条回答
  •  别跟我提以往
    2021-02-13 18:16

    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:

    
    

    And add it to your binding:

    Converter={StaticResource StringToUpperConverter}
    

提交回复
热议问题