#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.


        <Label Name="lblMA" Content="Margaret Atwood" HorizontalAlignment="Center"
               Padding="20,10" Margin="10"
        <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;



#338 – Setting a Control’s Foreground Color

We saw how to set the background of a control to a solid color by specifying a color value for the Background property.  Similarly, you can set the foreground color of a control using the Foreground property.  For controls that contain text, the foreground color is generally the color of the text.

Since the Foreground property’s type is System.Windows.Media.Brush, the property is set to an instance of a Brush object.  In XAML, you can set the property using the name of a predefined color, which maps to a predefined SolidColorBrush in the Brushes class.

        <Button Content="Take a Trip" Foreground="DarkViolet" Width="100" Margin="10"/>

Below is an image showing how some of the more common controls look when the Foreground property is set to a solid color.

#231 – You Can Use a Brush for a Control’s Foreground

Though brushes in WPF are most often used to fill in the background for a window or a control,  you can also render the foreground of many controls using a brush.

For example, we can use a brush instead of a solid color for the Foreground property of a Label.  The text of the label will be rendered in a specified font and the body of the letters will be rendered using the specified brush.

		<Label Content="You can render text with a brush.." HorizontalAlignment="Center" Margin="20"
			   FontSize="24" FontWeight="Bold">
				<LinearGradientBrush StartPoint="0,0.5" EndPoint="1.0,0.5">
					<GradientStop Color="DarkSlateBlue" Offset="0.0"/>
					<GradientStop Color="SkyBlue" Offset="1.0"/>