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

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

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

 

        
6条回答
  •  醉酒成梦
    2021-02-13 18:23

    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:

    
        
        
        
        

提交回复
热议问题