views:

23

answers:

3

I'm having trouble applying a template to my checkboxes. I have the following template:

<ControlTemplate x:Key="TriStateRed" TargetType="{x:Type CheckBox}">
    <ControlTemplate.Triggers>
        <Trigger Property="IsChecked" Value="{x:Null}" >
            <Setter TargetName="path" Property="Data" Value="M 0 2.5 A 5 2.5 0 1 1 5 5 L 5 8 M 5 10 L 5 10" />
            <Setter TargetName="path" Property="Stroke" Value="Red" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

However, visual studio gives me the following error:

Cannot find the Trigger target 'path'. (The target must appear before any Setters, Triggers, or Conditions that use it.)

Can anyone make sense of this?

A: 

It is looking for an element(control) named "path" inside the ControlTemplate. You need to have such element inside ControlTemplate in order to get this trigger working.

viky
A: 

It's likely because you want to create a border around your checkbox when IsChecked is null, but you haven't actually defined a Path element with the name "path".

Dave
A: 

It means WPF does not know what is "path" in Setters.

When you define ControlTemplate triggers like this, you need to specify visual elements that constitute checkbox and then name one of those elements "path" and then triggers will know on what element they are applied.

This looks to me like example code from MSDN magazine article: Using Templates to Customize WPF Controls. If this is so, you must use code from both Figure 1 and Figure 2 to have functioning CheckBox. There is code available for download or online browsing. Here is checkbox template: BigCheckBox.xaml

There is CheckBox ControlTemplate Example on MSDN.

Also, check this StackOverflow questions:

zendar