SystemColors 类提供对系统画笔和颜色(如 ControlBrush、ControlBrushKey 和 DesktopBrush)的访问权限。 系统画笔是使用指定的系统颜色来绘制区域的 SolidColorBrush 对象。 系统画笔总是生成纯色填充,它不能用于创建渐变。
您可以将系统画笔用作静态资源,也可以用作动态资源。 如果您希望在应用程序运行期间当用户更改系统画笔时画笔自动进行更新,请使用动态资源;否则请使用静态资源。 SystemColors 类包含有大量静态属性,这些属性遵循严格的命名约定:
*<系统颜色>*Brush
获取对指定的系统颜色的 SolidColorBrush 的静态引用。
*<系统颜色>*BrushKey
获取对指定的系统颜色的 SolidColorBrush 的动态引用。
*<系统颜色>*Color
获取对指定的系统颜色的 Color 结构的静态引用。
*<系统颜色>*ColorKey
获取对指定的系统颜色的 Color 结构的动态引用。
系统颜色是一种可用于配置画笔的 Color 结构。 例如,您可以通过使用系统颜色设置 LinearGradientBrush 对象的渐变停止点的 Color 属性来创建渐变。 有关示例,请参见如何:在渐变中使用系统颜色。
示例
下面的示例使用动态系统画笔引用来设置按钮的背景。
<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
WindowTitle="SystemColors Example" Background="White">
<StackPanel Margin="20">
<!-- Uses a dynamic resource to set the
background of a button.
If the desktop brush changes while this application
is running, this button will be updated. -->
<Button
Background="{DynamicResource {x:Static SystemColors.DesktopBrushKey}}"
Content="Hello, World!" />
</StackPanel>
</Page>
接下来的示例使用静态系统画笔引用来设置按钮的背景。
<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
WindowTitle="SystemColors Example" Background="White">
<StackPanel Margin="20">
<!-- Uses a static brush to set the
background of a button.
If the desktop brush changes while this application
is running, this button will not be updated until
the page is loaded again. -->
<Button
Background="{x:Static SystemColors.DesktopBrush}"
Content="Hello, World!" />
</StackPanel>
</Page>
有关演示如何在渐变中使用系统颜色的示例,请参见如何:在渐变中使用系统颜色。