If you’ve ever tried to set a foreground or background property for an object in WPF, you’ll notice that it takes an instance of the abstract type
Brush. This cryptic class cannot be instantiated; so how do we use it?
The best resource is the MSDN overview of WPF brushes. It lists various sub-classes of brushes (visually!):
I won’t go into too many details (since the MSDN page does a great job of that); suffice to say that the main brushes you need to know about are:
- SolidColorBrush: For a single, solid colour. Use
new SolidColorBrush(Color.FromRGB(r, g, b)).
- LinearGradientBrush: For a two-colour gradient. Use
new LinearGradientBrush(Color, Color, angle)(where angle = 0 for horizontal, 90 for vertical).
- ImageBrush: Uses an image as a (repeatable) pattern. Use
new ImageBrush(ImageSource)(where ImageSource is probably a BitmapSource or DrawingImage).
Best of all is that, like the rest of WPF, these are vector-style brushes designed to scale up or down with your application control size. So use them with impunity, but test, test, test!