不是WPF主题为什么要使用systemColors中? [英] Why don't WPF themes use SystemColors?

查看:897
本文介绍了不是WPF主题为什么要使用systemColors中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

综观可供WPF的各种开源的主题,我有点惊讶地看到,颜色不映射到系统颜色。所以,如果你改变你的整个系统的配置,这些主题不会改变,以适应。

Looking at the various open source themes available for WPF, I'm a bit surprised to see that colours aren't mapped to system colours. So if you change your overall system configuration, these themes won't change to suit.

是否有正确使用系统颜色,如果是这样,我怎么写一个按钮样式,将利用这些颜色?任何主题

Are there any themes that properly use the system colours, and if so, how do I write a button style that will leverage those colours?

要举一个例子,这里有一个按钮的风格:

To give an example, here's the style of a button:

    <Style x:Key="specialButton" TargetType="Button">
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
        <Setter Property="Background" Value="{StaticResource NormalBrush}"/>
        <Setter Property="Margin" Value="2"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Name="border"
                    BorderThickness="1"
                    Background="{TemplateBinding Background}"
                    BorderBrush="{StaticResource NormalBorderBrush}"
                    Padding="1,1">
                        <ContentPresenter Name="content" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="border" Property="BorderBrush" Value="{StaticResource DarkBrush}"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="border" Property="Background" Value="{StaticResource PressedBrush}"/>
                            <Setter TargetName="border" Property="BorderBrush" Value="{StaticResource PressedBorderBrush}"/>
                        </Trigger>
                        <Trigger Property="IsDefaulted" Value="True">
                            <Setter TargetName="border" Property="BorderBrush" Value="{StaticResource DefaultedBorderBrush}"/>
                        </Trigger>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter TargetName="border" Property="BorderBrush" Value="{StaticResource DefaultedBorderBrush}"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/>
                            <Setter TargetName="border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}"/>
                            <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

问题是,如何让他们挂接到系统颜色指定命名静态资源?例如,背景可能是这样的:

The question is, how to specify the named static resources so that they hook into the system colours? For example, the background might look like this:

    <LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="{DynamicResource {x:Static SystemColors.ControlLightLightColorKey}}" Offset="0.0"/>
                <GradientStop Color="{DynamicResource {x:Static SystemColors.ControlLightColorKey}}" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

不过,这看上去完全像一个标准的按钮。如果我使用XAMLPAD检查一个按钮,我可以看到以下内容:

However this looks nothing like a standard button. If I use XAMLPAD to examine a button, I can see the following:

     <LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#FFF3F3F3" Offset="0.0"/>
                <GradientStop Color="#FFEBEBEB" Offset="0.5"/>
                <GradientStop Color="#FFDDDDDD" Offset="0.5"/>
                <GradientStop Color="#FFCDCDCD" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

使用的颜色似乎是常量,而不是根据系统的色彩。

The colours used appear to be constants, and not based on System Colours.

推荐答案

我不知道第三方的主题,但系统颜色XAML中都可以通过的 systemColors中 类。此类公开,可以在您的XAML两种方式之一使用的资源键:

I don't know about third-party themes, but the system colours are available in Xaml through the SystemColors class. This class exposes the keys of resources that can be used in your xaml in one of two ways:

<Border Background="{x:Static SystemColors.ControlDarkBrushKey}" />

<Border Background="{StaticResource {x:Static SystemColors.ControlDarkBrushKey}}" />

上面的每条应该给你一个背景相同系统的 ControlDarkBrush 颜色的边框。我给的文档链接提供可用资源的完整列表。

Either of the above should give you a border with a background the same as the system's ControlDarkBrush colour. The documentation link I gave supplies the full list of available resources.

这是值得注意的是, systemColors中为您提供既具有可用的每种颜色,并且颜色本身 - 所以如果你想创建自己的画笔,从一个系统颜色变淡到另一个,你可以轻松地创建这一点。

It's worth noting that SystemColors provides you with both a Brush for each colour available, and the colour itself - so if you want to create your own brush that fades from one system colour to another, you can create this easily.

这篇关于不是WPF主题为什么要使用systemColors中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆