Search code examples
wpfsilverlightxamlgraphicscolors

Difference between Color and SolidColorBrush clarification


Ok this has been bugging me and I haven't really found any kind of definitive answer as the the reason/cause of the difference between Color & SolidColorBrush so I'm wondering if someone can educate me on this.

I already know the differences in usage, like for example I can use a SolidColorBrush in a dependency like if I say;

<SolidColorBrush x:Key="BlahBrush" Color="#FFFFFFFF"/>
<Border Background="{StaticResource BlahBrush}"/>

but then say I throw the same resource in an EasingColorKeyFrame kind of like;

<EasingColorKeyFrame KeyTime="0" Value="{StaticResource BlahBrush}" />

then it's going to puke at me about it being a SolidColorBrush....except then I can get around that by just declaring it via a resource chain back to a Color kind of like;

<Color x:Key="OriginalBlahBrush">#FFFFFFFF</Color>
<SolidColorBrush x:Key="BlahBrush" Color="{StaticResource OriginalBlahBrush}"/>

and it will be just fine....but then again I could just utilize the Color property alone of the SolidColorBrush and can get the same behavior without being separated kind of like;

<SolidColorBrush>
     <SolidColorBrush.Color>
        <Color A="255" R="0" G="0" B="255" />
     </SolidColorBrush.Color>
</SolidColorBrush>

So I guess my question is, what is the inherent difference between the Colors and SolidColorBrush class's and the reason for their weird quirks in usage? aka I guess what's the reason for System.Windows.Media.Colors vs System.Windows.Media.SolidColorBrush if they're both just giving a solid damn color??

Inquiring minds want to know! :)


Solution

  • From the Remarks section in Brush:

    A Brush "paints" or "fills" an area with its output. Different brushes have different types of output. Some brushes paint an area with a solid color, others with a gradient, pattern, image, or drawing. The following list describes the different types of WPF brushes:

    •SolidColorBrush: Paints an area with a solid Color.

    •LinearGradientBrush: Paints an area with a linear gradient.

    •RadialGradientBrush: Paints an area with a radial gradient.

    •ImageBrush: Paints an area with an image (represented by an ImageSource object).

    •DrawingBrush: Paints an area with a Drawing. The drawing may include vector and bitmap objects.

    •VisualBrush: Paints an area with a Visual object. A VisualBrush enables you to duplicate content from one portion of your application into another area; it's very useful for creating reflection effects and magnifying portions of the screen.