문제

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