WPF SolidColorBrush
WPF SolidColorBrush
WPF SolidColorBrush
We can also set the Color property to the hexadecimal code of the color. The following code snippet sets the color of the SolidColorBrush to blue using the hexadecimal value of blue color.
<SolidColorBrush Color="#FF0000FF" />
Figure 13 taken from MSDN shows the properties and hexadecimal values of the properties of the Colors class.
Figure 13. Colors class properties As we have seen in the previous sections, the Fill property of shape paints the internal areas of a shape. We can direct specify a color in the Fill property and the default brush used to fill a shape is solid brush. The code snippet in Listing 12 creates a rectangle shape sets the Fill property to Blue.
<Rectangle Width="200" Height="100" Stroke="Black" StrokeThickness="4" Fill="Blue">
</Rectangle>
Figure 14. A shape filled with a solid color We can simply replace Listing 12 with the Listing 13 where we set Rectangle.Fill property to a SolidColorBrush with a blue color.
<Rectangle Width="200" Height="100" Stroke="Black" StrokeThickness="4"> <Rectangle.Fill> <SolidColorBrush Color="Blue" /> </Rectangle.Fill> </Rectangle>
Listing 13 The CreateARectangleWithSolidBrush method listed in Listing 14 draws same rectangle in Figure 13 dynamically.
/// <summary> /// Creates a blue rectangle with black border /// </summary>
public void CreateARectangleWithSolidBrush() { // Create a Rectangle Rectangle blueRectangle = new Rectangle(); blueRectangle.Height = 100; blueRectangle.Width = 200;
// Create a blue and a black Brush SolidColorBrush blueBrush = new SolidColorBrush(); blueBrush.Color = Colors.Blue; SolidColorBrush blackBrush = new SolidColorBrush(); blackBrush.Color = Colors.Black;
// Set Rectangle's width and color blueRectangle.StrokeThickness = 4; blueRectangle.Stroke = blackBrush; // Fill rectangle with blue color blueRectangle.Fill = blueBrush;
Listing 14
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.