views:

1270

answers:

3

I am trying to do something very simple. I have a ToggleButton.IsChecked property bound to a bool. I want the background to toggle between red(false) and green(true). But for some reason it seems to be toggling between red and no background. I used a converter to check if I am getting proper notifications from the source and I am, so not sure why one trigger(false/red) works and the other(true/green) doesnt. Also would like to hear how people debug these kind of issues. Thanks!

Here is the code.

<DataTemplate x:Name"Flipper">
    <StackPanel>
    ...
    <ToggleButton IsChecked="{Binding Path=BoolValue,
                                      Converter={StaticResource converter}}" 
                  Name="onoff" >
    </ToggleButton>
    ...
    <StackPanel>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding ElementName=onoff,Path=IsChecked}"
                     Value="True">
            <Setter TargetName="onoff" Property="Background" Value="Green"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding ElementName=onoff,Path=IsChecked}" 
                     Value="False">
            <Setter TargetName="onoff" Property="Background" Value="Red"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

Update: I changed the togglebutton to a checkbox and it works. No idea why...

A: 

It looks ok to me, can you try altering the converter to return "red" or "green" rather than True/False (and alter the trigger accordingly). I have seen some wierd behaviour with WPF triggers when using NULL or Booleans in that it "unsets" the property if it's the opposite of your trigger value, rather than using another trigger value.

As for debugging them.. I'd love to know if there's a better way than the hack and hope methods I generally use for XAML debugging :D

Steven Robbins
Thanks for the suggestion. I tried it but no luck. I dont understand why one trigger seems to be firing and not the other.
Klerk
Try putting your converter inside the datatrigger itself and returning a straight bool from it, rather than the Nullable<bool> that ischecked returns.
Steven Robbins
Also make sure IsThreeState is false.
Steven Robbins
+1  A: 

I wondered myself how hard this would be and a quick google gave the following answers:

Cohen
+1  A: 

Here's a useful technique for debugging triggers effectively:

http://www.wpfmentor.com/2009/01/how-to-debug-triggers-using-trigger.html