WPF Debugging datatriggers?
Asked Answered
J

2

15

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...

Jinny answered 5/1, 2009 at 13:40 Comment(1)
Does this answer your question? In WPF, how to debug triggers?Aftersensation
U
7

Try using WPF Inspector:

https://wpfinspector.codeplex.com/

Once you attach to your running WPF application, highlight the element in question by holding down ctrl + clicking on it. Then, select the element in the visual tree (might be a parent) that contains the trigger. Click on the triggers tab and you can see the current evaluation (e.g. True == True). If the datatrigger condition is met, the little icon will be orange (lit).

Ur answered 3/5, 2011 at 21:32 Comment(6)
I tried to use this but my application did not show up in the list of WPF applications. Do you have any hints for getting around this issue?Eleanor
Check out the source. It looks like an application is added to the list if GetIsManagedApplication returns true and the process name doesn't contain "devenv", "PresentationHost", or "inspector". Perhaps your project's name contains one of those strings? Otherwise, look into the code that determines if an application is managed.Ur
Yep, it contains "Inspector" ... that seems like a pretty hacky way to avoid listing his own application, but I guess I could modify the source. For now I just used @DanLampings answer.Eleanor
Agreed. At a minimum, the code should have checked against "wpf inspector"Ur
My app also dose not show in the list. My app name dose not contains any of these strings...Offertory
Does your app reference PresentationCore or PresentationFramework? If so, then your best bet is to download the source and set some breakpoints in ManagedApplicationsService.cs.Ur
S
1

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

Salbu answered 5/1, 2009 at 14:12 Comment(3)
Thanks for the suggestion. I tried it but no luck. I dont understand why one trigger seems to be firing and not the other.Jinny
Try putting your converter inside the datatrigger itself and returning a straight bool from it, rather than the Nullable<bool> that ischecked returns.Salbu
Also make sure IsThreeState is false.Salbu

© 2022 - 2024 — McMap. All rights reserved.