USING BRUSHES

Now that you have an understanding of how to manipulate and use colors in Silverlight, it's time to take a more in-depth look at the Brush object, along with all the available types of brushes that can be used to fill Silverlight controls.

The SolidColorBrush Object

As you've seen in the previous code samples, the SolidColorBrush object is used primarily to fill a shape or control with a single solid color. You can use any of the available runtime colors in XAML or in the code-behind that you've seen before. In the following code, you set the Fill property of a Rectangle shape to a blue SolidColorBrush object, resulting in the rectangle being filled as shown in Figure 3-10:

FIGURE 3-10: A SolidColorBrush object.

image

MainRect.Fill = new SolidColorBrush(Colors.Blue);

The LinearGradientBrush Object

The LinearGradientBrush is a Brush object that enables you to create horizontal, vertical, and diagonal gradients to use for filling objects via the Background or Fill property. Like the SolidColorBrush, it is easy to use in XAML or in the code-behind. To fill the same Rectangle object used in the preceding section with a horizontal gradient, you can use XAML code such as the following:

<Rectangle x:Name=“MainRect”Height=“300”Width=“300”> <Rectangle.Fill> <LinearGradientBrush StartPoint=“0,0.5”EndPoint=“1,0.5”> <GradientStop Color=“Black”Offset=“0.0”/> <GradientStop Color=“White”Offset=“0.5”/> ...

Get Beginning Windows® Phone 7 Application Development: Building Windows® Phone Applications Using Silverlight® and XNA® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.