SolidColorBrush 类
使用纯色绘制区域。
程序集: System.Windows(在 System.Windows.dll 中)
[ContentPropertyAttribute("Color", true)] public sealed class SolidColorBrush : Brush
<SolidColorBrush .../>
-or-
<SolidColorBrush>colorString</SolidColorBrush>
<object property="predefinedColor"/>
- or -
<object property="#rgb"/>
- or -
<object property="#argb"/>
- or -
<object property="#rrggbb"/>
- or -
<object property="#aarrggbb"/>
- or -
<object property="sc#scR,scG,scB"/>
- or -
<object property="sc# scA,scR,scG,scB"/>
XAML 值
SolidColorBrush 是最基本的画笔,用于将外观应用于对象。在 XAML 中,可以通过类型转换语法(该语法对关于指定颜色的字符串的意义使用了多个约定),将 SolidColorBrush 指定为属性值。其他画笔(如 LinearGradientBrush)需要属性元素语法。可以使用 SolidColorBrush 的对象元素语法,它适用于以下情况:
可以使用 ColorAnimation 或 ColorAnimationUsingKeyFrames,对 SolidColorBrush 进行动画处理。这通常不是通过对 SolidColorBrush 的 Color 属性进行动画处理实现的,而是通过使用 Shape..::.Fill 等属性(采用了 Brush)的间接目标实现的。ColorAnimation 参考主题演示了一个示例。
SolidColorBrush 是作为类型转换语法的一部分创建的,该语法由 Brush 类和 SolidColorBrush 本身启用。该语法在以上"语法"部分的"XAML 属性用法"小节中进行了说明。使用此语法,您可以为采用 Brush 的属性指定一个字符串值,该字符串在多个可能的约定中进行解释,这些约定包含命名颜色、RGB、ScRGB。RGB 和 ScRBG 都可以指定 alpha 值。有关 XAML 语法的更多信息,请参见 Color 和 Brush。
在代码中设置 SolidColorBrush 的预定义颜色
在任何显示技术中,最常见的一个操作就是使用纯色绘制区域。为了完成这个任务,Silverlight 提供了 SolidColorBrush 类。以下各节介绍使用 SolidColorBrush 进行绘制的不同方式。
若要在 XAML 中用纯色绘制区域,请使用以下选项之一:
-
通过名称选择一个预定义的 SolidColorBrush。例如,可以将 Rectangle 的 Fill 设置为 Red 或 MediumBlue。该示例使用预定义 SolidColorBrush 的名称来设置 Rectangle 的 Fill。
-
通过指定红色、绿色和蓝色的分量以组合成单一纯色,从 32 位调色板中选择一种颜色。从 32 位调色板中指定一种颜色时使用的格式为 #rrggbb,其中 rr 是指定红色相对量的两字符十六进制数,gg 指定绿色相对量,bb 指定蓝色相对量。此外,还可以按 aarrggbb 格式指定颜色,其中 aa 指定颜色的 alpha 值或透明度。通过此方法可以创建部分透明的颜色。在下面的示例中,使用十六进制表示法将 Rectangle 的 Fill 设置为完全不透明的红色。
-
使用属性元素语法可描述 SolidColorBrush。此语法更为详细,可用于指定其他设置,如画笔的不透明度。在下面的示例中,两个 Rectangle 元素的 Fill 属性设置为完全不透明的红色。第一支画笔的颜色使用预定义的颜色名称进行描述。第二支画笔的颜色使用十六进制表示法进行描述。
XAML<StackPanel> <!-- Both of these rectangles' fills are painted with red SolidColorBrush objects, described using object element syntax. --> <Rectangle Width="100" Height="100"> <Rectangle.Fill> <SolidColorBrush Color="Red" /> </Rectangle.Fill> </Rectangle> <Rectangle Width="100" Height="100" Canvas.Top="110"> <Rectangle.Fill> <SolidColorBrush Color="#FFFF0000" /> </Rectangle.Fill> </Rectangle> </StackPanel>
System.Windows..::.DependencyObject
System.Windows.Media..::.Brush
System.Windows.Media..::.SolidColorBrush