Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

<TextBlock Name="tbAbc"
           FontSize="12"
           TextAlignment="Center"
           Text="Channel Name"
           Foreground="{DynamicResource {x:Static r:RibbonSkinResources.RibbonGroupLabelFontColorBrushKey}}" />

The strings are taken through Binding. I don't want to make the strings uppercase in the dictionary itself.

like image 751
feralbino Avatar asked Jul 25 '14 12:07

feralbino


People also ask

How do you make TextBox use all uppercase or lowercase characters?

In properties of TextBox simply set CharacterCasing to Upper. It'll convert all entered character in uppercase. Save this answer.

What is the difference between TextBlock and label in WPF?

Labels usually support single line text output while the TextBlock is intended for multiline text display. For example in wpf TextBlock has a property TextWrapping which enables multiline input; Label does not have this.

Is TextBlock editable?

TextBlocks can be edited by users using the TextEditingTool. The HTMLInfo that a given TextBlock uses as its text editor can be customized by setting the textEditor property.

What is TextBlock in XAML?

TextBlock is the primary control for displaying read-only text in apps.


2 Answers

Or use

Typography.Capitals="AllSmallCaps"

in your TextBlock definition.

See here: MSDN - Typography.Capitals

EDIT:

This does not work in Windows Phone 8.1, only in Windows 8.1 ...

like image 158
TheEye Avatar answered Sep 20 '22 00:09

TheEye


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:

<local:StringToUpperConverter  x:Key="StringToUpperConverter"/>

And add it to your binding:

Converter={StaticResource StringToUpperConverter}
like image 21
kidshaw Avatar answered Sep 21 '22 00:09

kidshaw