简体   繁体   中英

Change colors in a WPF CheckBox

I would like to specify particular colours for the border and background for a WPF checkbox. 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)...

<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. 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. 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. It seams easiest copying the original template and changing it to the way you like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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