#812 – Use TextDecorations Property to Underline or Strike Through Text

Some controls that render text include a TextDecorations property that allows you to underline the text or draw a line through the middle of the text.

The allowed values of the TextDecorations property are:

  • Underline – Underline the text (line appears just a bit below the bottom of the characters)
  • Baseline – Draw a line that runs along the bottom edge of the characters (just above where the underline would appear)
  • StrikeThrough – Draw a line through the middle of the characters
  • Overline – Draw a line over the top edge of the characters

Below is an example of the TextDecorations property used with the TextBlock control.  TextDecorations is not supported for Labels.

        <TextBlock Text="Underline pphh" Padding="20,10" FontSize="16"
                   TextDecorations="Underline"/>
        <TextBlock Text="Baseline pphh" Padding="20,10" FontSize="16"
                   TextDecorations="Baseline"/>
        <TextBlock Text="StrikeThrough pphh" Padding="20,10" FontSize="16"
                   TextDecorations="StrikeThrough"/>
        <TextBlock Text="Overline pphh" Padding="20,10" FontSize="16"
                   TextDecorations="Overline"/>

812-001

#811 – Setting Color Values in Code Based on System Colors

You can set the Foreground or Background properties of a control to a brush that will render the control using one of the predefined system colors.  The SystemColors class contains a number of static SolidColorBrush objects representing brushes that match the system colors.

811-001

        <Label Name="lblMA" Content="Margaret Atwood" HorizontalAlignment="Center"
               Padding="20,10" Margin="10"
               Background="LightPink"/>
        <Button Content="Change Color" HorizontalAlignment="Center"
                Padding="10,5" Click="Button_Click"/>

 

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // Set to one of the predefined brushes that map
            // to the system colors
            lblMA.Background = SystemColors.ActiveCaptionBrush;
        }

811-002
811-003

#810 – Setting Foreground and Background Properties from Code

You can set both the Foreground and Background properties at run-time, from code.  Recall that both properties are set to an instance of a Brush, which can be a SolidColorBrush,  one of the GradientBrush subtypes, or one of several other different types of Brush objects.

The simplest way to change a Foreground or Background property, assuming that you want to set them to a solid color, is to set the property to refer to one of the preexisting SolidColorBrush objects that are part of the Brushes class.

The Brushes class includes a set of static SolidColorBrush objects, each representing one of the standard predefined colors.

        <Label Name="lblMA" Content="Margaret Atwood" HorizontalAlignment="Center"
               Padding="20,10" Margin="10"
               Background="LightPink"/>
        <Button Content="Change Color" HorizontalAlignment="Center"
                Padding="10,5" Click="Button_Click"/>

810-001

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // Set to one of the predefined brushes
            lblMA.Background = Brushes.Plum;
        }

810-002