How to make all text upper case / capital?

前端 未结 7 1592
Happy的楠姐
Happy的楠姐 2020-12-03 10:01

I want all texts in TextBlock, Label, MenuItem.Header to be displayed in upper case. The strings are taken from a ResourceDictio

相关标签:
7条回答
  • 2020-12-03 10:30

    This does not strictly answer the question but does provide a trick to cause the same effect.

    I believe many finding their way here are looking how to do this with a style. TextBlock is a bit tricky here because it is not a Control but a FrameworkElement and therefore you can not define a Template to do the trick.

    The need to use all uppercase text is most likely for headings or something like that where use of Label is justified. My solution was:

    <!-- Examples of CaseConverter can be found in other answers -->
    
    <ControlTemplate x:Key="UppercaseLabelTemplate" TargetType="{x:Type Label}">
        <TextBlock Text="{TemplateBinding Content, Converter={StaticResource CaseConverter}}" />
    </ControlTemplate>
    
    <Style x:Key="UppercaseHeadingStyle"
           TargetType="{x:Type Label}">
        <Setter Property="FontSize" Value="20" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="Template" Value="{StaticResource UppercaseLabelTemplate}" />
    </Style>
    
    <!-- Usage: -->
    <Label Content="Header" Style="{StaticResource UppercaseHeadingStyle}" />
    

    Note that this does disable some of the default behavior of Label, and works only for text, so I would not define this as default (no one probably wants all labels uppercase anyway). And of course you must use Label instead of TextBlock when you need this style. Also I would not use this inside of other templates, but only strictly as a topic style.

    0 讨论(0)
提交回复
热议问题