Home > DeveloperSection > Forums > Style triggers produce no effect
Babe Zaharias
Babe Zaharias

Total Post:19

Points:133
Posted on    July-15-2013 3:57 AM

 WPF WPF 
Ratings:


 1 Reply(s)
 848  View(s)
Rate this:
Hello MindStick

Every time I think I finally get WPF/XAML something unintuitive like this come up (rant over)...

I want to implement a "poor's man" toggle button for a menu item. Essentially, when it's checked it should say "Metric" and when it's unchecked - "Imperial". I thought it should be fairly straightforward with XAML, but I can't get it to work.

Here's what I tried:

<MenuItem IsCheckable="True" IsChecked="True">
    <Style>
       <Style.Triggers>
           <Trigger Property="IsChecked" Value="True">
              <Setter Property="Header" Value="Metric"/>
           </Trigger>
           <Trigger Property="IsChecked" Value="False">
              <Setter Property="Header" Value="Imperial"/>
           </Trigger>
       </Style.Triggers>
     </Style>
</MenuItem>

It gives me an error "The member 'IsChecked' is not recognized or is not accssible" (same for Header).

The error goes away when I do <Style TargetType="{x:Type MenuItem}">, but it doesn't do anything.

I tried directly without the <Style>

<MenuItem.Triggers>
   <Trigger...>
</MenuItem.Triggers>

First, it gave me the same error as above, but when I changed the property to Property="MenuItem.IsChecked" it started screaming that only EventTriggers are allowed.

I also tried with a DataTrigger (granted, I still don't fully understand the differences between the triggers):

<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type MenuItem}}, Path=IsChecked}" Value="True">
    <Setter Property="Header" Value="Metric"/>
</DataTrigger>

No effect.

Help... please...

Thanks in advance



shreesh chandra shukla
shreesh chandra shukla

Total Post:105

Points:735
Posted on    July-15-2013 7:55 AM

Hey  Babe Zaharias !

You have to assign the MenuItem's Style property by declaring the <Style> as child element of a <MenuItem.Style> element.

<MenuItem IsCheckable="True" IsChecked="True">
    <MenuItem.Style>
        <Style TargetType="MenuItem">
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Header" Value="Metric"/>
                </Trigger>
                <Trigger Property="IsChecked" Value="False">
                    <Setter Property="Header" Value="Imperial"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </MenuItem.Style>
</MenuItem>


Don't want to miss updates? Please click the below button!

Follow MindStick