1

When i add this controltemplate my checkboxes disapear and only the checkbox text is shown. And when mouse is over the textbackground becomes red, how do i get my checkbox background red when mouse is over?

<Style TargetType="CheckBox" x:Key="Checkbox">
    <Setter Property="BorderThickness"          Value="2" />
    <Setter Property="VerticalAlignment"        Value="Top" />
    <Setter Property="Margin"                   Value="20,15,0,0" />
    <Setter Property="FontFamily"               Value="/Resources/Fonts/Source Sans Pro/#Source Sans Pro" />
    <Setter Property="FontSize"                 Value="14" />
    <Setter Property="HorizontalAlignment"      Value="Left" />
    <Setter Property="Foreground"               Value="{DynamicResource CheckboxForegroundColor}" />
    <Setter Property="Background"               Value="{DynamicResource CheckboxBackgroundColor}" />
    <Setter Property="BorderBrush"              Value="{DynamicResource CheckboxBorderbrushColor}"/>


    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CheckBox}">
                <CheckBox  Background="{TemplateBinding Background}" >
                    <ContentPresenter  />
                </CheckBox>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Red"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
Saadi
  • 2,211
  • 4
  • 21
  • 50
A.Game
  • 49
  • 6

1 Answers1

1

This will work for some properties (try FontSize, for example), but not for others. The default template for the CheckBox has triggers of its own, and they don't bother with such things as TemplateBindings.

This post tells you how to extract the default template for a ComboBox. Here is an excerpt which shows your problem:

<ControlTemplate.Triggers>
    ...
    <Trigger Property="IsMouseOver" Value="true">
        <Setter
            Property="Background"
            TargetName="checkBoxBorder"
            Value="{StaticResource OptionMark.MouseOver.Background}"/>
        ...
    </Trigger>
    ...
</ControlTemplate.Triggers>

Your best bet is probably to override the entire template. Happily, it isn't too complex...


  1. Remove your own template.
  2. Follow the steps outlined in the post I linked to. That should give you a copy of the default CheckBox template.
  3. Modify that copy. In particular, the IsMouseOver trigger that I excerpted: Change {StaticResource OptionMark.MouseOver.Background} to Red.

If this is still unclear, see this related post.

Community
  • 1
  • 1
Petter Hesselberg
  • 5,062
  • 2
  • 24
  • 42