Difference between Color and SolidColorBrush clari

2020-04-08 13:43发布

问题:

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! :)

回答1:

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.



回答2:

Color is a component of SolidColorBrush and one of it constructor has Color-type parametr. If you want to fill some components on the forms first you need to create color and after create SolidColorBrush based on that color which you was created earlier. It's like a real clear brush and color palette, where you need to soak the brush for painting/filling something. Color object is more "low-level" and you can set some color with A, R, G, B-parameters. And SolidColorBrushes is a type of brushes for filling object (inheritance from System.Windows.Media.Brush). You can combine colors in one Brush.



回答3:

My understanding is the following.

  • Color represents a specific color { red, blue, … } etc.
  • SolidColorBrush represents specific Color and the ability to use the color to paint an area.
  • Color is a struct data type that holds the details of color.
  • SolidColorBrush is a class that holds Color objects and adds Opacity and transformation properties.
  • Colors list pre-set number of colors. Color c = Colors.Red;
  • Brushes Lists pre-set number of Brushes. SolidColorBrush br = Brushes.Red;