如何:在渐变中使用系统颜色
若要在渐变中使用系统颜色,应使用 SystemColors 类的 *<SystemColor>Color 和 *<SystemColor>*ColorKey 静态属性来获取对颜色的引用,其中 <SystemColor> 是所需系统颜色的名称。 当希望创建随系统主题变化而自动更新的动态引用时,请使用 *<SystemColor>ColorKey* 属性。 否则,应使用 <SystemColor>*Color 属性。
示例
以下示例使用动态系统颜色资源创建渐变。
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowTitle="Dynamic System Colors Example" Background="White">
<StackPanel Margin="20">
<!-- Uses dynamic references to system colors to set
the colors of gradient stops.
If these system colors change while this application
is running, the gradient will be updated
automatically. -->
<Button Content="Hello, World!">
<Button.Background>
<LinearGradientBrush>
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.0"
Color="{DynamicResource {x:Static SystemColors.DesktopColorKey}}" />
<GradientStop Offset="1.0"
Color="{DynamicResource {x:Static SystemColors.ControlLightLightColorKey}}" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Button.Background>
</Button>
</StackPanel>
</Page>
下一个示例使用静态系统颜色资源创建渐变。
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowTitle="Static System Colors Example" Background="White">
<StackPanel Margin="20">
<!-- Uses static references to system colors to set
the colors of gradient stops.
If these system colors change while this application
is running, this button will not be updated until
the page is loaded again. -->
<Button Content="Hello, World!">
<Button.Background>
<LinearGradientBrush>
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.0"
Color="{x:Static SystemColors.DesktopColor}" />
<GradientStop Offset="1.0"
Color="{x:Static SystemColors.ControlLightLightColor}" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Button.Background>
</Button>
</StackPanel>
</Page>