简体   繁体   English

在WPF复选框中更改颜色

[英]Change colors in a WPF CheckBox

I would like to specify particular colours for the border and background for a WPF checkbox. 我想为WPF复选框的边框和背景指定特定的颜色。 All the examples I've seen so far seem to be too complicated as they involve changing everything - I just want to change the colors. 到目前为止,我所看到的所有示例似乎都太复杂了,因为它们涉及到更改所有内容-我只想更改颜色。

This is what I have so far - the colors are correct but the checkbox has shrunk (cant see the 'tick' symbol)... 这是我到目前为止的内容-颜色正确,但是复选框缩小了(不能看到“ tick”符号)...

<Style TargetType="CheckBox">
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Padding" Value="0" />
    <Setter Property="Background" Value="#666666" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="CheckBox">
                <Border Background="{TemplateBinding Background}" 
                        BorderThickness="1" 
                        BorderBrush="Aquamarine">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsChecked" Value="False">
            <Setter Property="BorderBrush" Value="Aquamarine"/>
        </Trigger>
        <Trigger Property="IsChecked" Value="True">
            <Setter Property="BorderBrush" Value="Aquamarine" />
        </Trigger>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="#ffff00"/>
            <Setter Property="BorderBrush" Value="Aquamarine"/>
        </Trigger>
    </Style.Triggers>
</Style>

The problem is that ControlTemplates don't work as Styles. 问题在于ControlTemplates不能用作样式。 As you can't zoom in on a particular element, you need to declare everything each time. 由于无法放大特定元素,因此需要每次声明所有内容。 For example, you can't see the 'tick' symbol because you left out the dimensions for the border. 例如,您看不到“ tick”符号,因为省略了边框的尺寸。 This is easily fixed: 这很容易解决:

<Border Background="{TemplateBinding Background}" 
        BorderThickness="1" 
        BorderBrush="Aquamarine"
        Width="13"
        Height="13"
        >

But the main problem remains, you won't get a functional CheckBox. 但是主要问题仍然存在,您将无法使用功能强大的CheckBox。 It seams easiest copying the original template and changing it to the way you like. 它看起来很容易复制原始模板并将其更改为您喜欢的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM