Question

I have a specific TextBlock in a WPF application. I need to make the text Uppercase for that specifc TextBlock.

Trying with the following code I get this error:

{"'TextUpperCase' is not a valid value for property 'Style'."}

Any idea how to solve it?

  <Style x:Key="TextUpperCase" TargetType="{x:Type TextBox}">
        <Setter Property="CharacterCasing" Value="Upper"/>
    </Style>


                <TextBlock
                    x:Name="ShopNameTextBlock"
                    TextWrapping="Wrap"
                    Text="{Binding Description, FallbackValue=Shop name}"
                    Style="TextUpperCase"
                    VerticalAlignment="Center" 
                    FontFamily="/GateeClientWPF;component/Fonts/#Letter Gothic L"
                    FontSize="45"
                    Grid.ColumnSpan="2"
                    Margin="0,60,0,0"
                    FontWeight="Medium"
                    TextAlignment="Center"
                    Foreground="Black"
                    />
Was it helpful?

Solution

CharacterCasing is not valid property for TextBlock, it's for TextBox.

You can have IValueConverter and use it with your binding which will convert text to Upper.


Declare Converter:

public class ToUpperValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
                          CultureInfo culture)
    {
        if (value is string)
        {
           return value.ToString().ToUpper();
        }
        return String.Empty;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
                              CultureInfo culture)
    {
        return Binding.DoNothing;
    }
}

Now, add a reference of your converter in XAML and use like this:

<TextBlock Text="{Binding Description,
                  Converter={StaticResource ToUpperValueConverter}}"/>

OTHER TIPS

to use an style, you must first declare in a UserControl.Resources:

<UserControl.Resources>
        <Style x:Key="TextUpperCase" TargetType="{x:Type TextBox}">
            <Setter Property="CharacterCasing" Value="Upper"/>
        </Style>
</UserControl.Resources>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top