Thursday, August 19, 2010

Adding a SolidColorBrush in Silverlight

Adding a SolidColorBrush


The SolidColorBrush is the most common type that you can apply to Silverlight objects. A SolidColorBrush is automatically created for you when you set the color in one of the properties of a Silverlight object. For example, the following code automatically creates a SolidColorBrush implicitly for the Background property of a Canvas control:

< Canvas Background="Lavender"
Height="200"
Width="200" >
< / Canvas >

The previous code could also be written explicitly as follows:

< Canvas Height="200"
Width="200" >
< Canvas.Background >
< SolidColorBrush Color="Lavender"/ >
< /Canvas.Background >
< /Canvas >

Adding the SolidColorBrush explicitly takes considerably more code; however, you can set some additional properties of the SolidColorBrush element to enhance the look and feel of the control. For example, you can make the SolidColorBrush appear semitransparent by setting the Opacity property, as shown in the following code:

< Canvas Height="200"

Width="200" >
< Canvas.Background >
< SolidColorBrush Color="Red" Opacity=".3"/ >
< /Canvas.Background >
< /Canvas >

No comments: