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"
                    />
有帮助吗?

解决方案

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}}"/>

其他提示

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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top