如何在WPF中的TextBlock上应用CharacterCasing?

Gib*_*boK 2 .net wpf xaml wpf-controls

我在WPF应用程序中有一个特定的TextBlock.我需要为该特定的TextBlock创建大写文本.

尝试使用以下代码我收到此错误:

{"'TextUpperCase' is not a valid value for property 'Style'."}
Run Code Online (Sandbox Code Playgroud)

知道怎么解决吗?

  <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"
                    />
Run Code Online (Sandbox Code Playgroud)

Roh*_*ats 8

CharacterCasing不是有效的财产TextBlock,它是TextBox.

您可以使用IValueConverter并将其与您的绑定结合使用,该绑定将文本转换为Upper.


声明转换器:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,在XAML中添加转换器的引用并使用如下:

<TextBlock Text="{Binding Description,
                  Converter={StaticResource ToUpperValueConverter}}"/>
Run Code Online (Sandbox Code Playgroud)

  • @RohitVats有什么通用的方法可以将TextBlock的文本更改为大写吗?我的意思是我希望所有TextBlock / ListBoxItems都使用大写。这可能吗? (2认同)