Thursday, January 22, 2009

How to debug triggers using Trigger-Tracing

Debugging triggers is a painful process: they work behind the scenes, there's nowhere to put a breakpoint and no call-stack to help you. The usual approach taken is trial and error based and it nearly always takes longer than it should to work out what's going wrong.

This post describes a new technique for debugging triggers allowing you to log all trigger actions along with the elements being acted upon:


It's good because it:
  • helps you fix all manner of problems :)
  • works on all types of trigger: Trigger, DataTrigger, MultiTrigger etc.
  • allows you to add breakpoints when any trigger is entered and/or exited
  • is easy to set up: just drop one source file (TriggerTracing.cs) into your app and set these attached properties to the trigger to be traced:
    <Trigger my:TriggerTracing.TriggerName="BoldWhenMouseIsOver"
             my:TriggerTracing.TraceEnabled="True"
             Property="IsMouseOver"
             Value="True">
        <Setter Property="FontWeight" Value="Bold"/>
    </Trigger>
    

It works by:
  • using attached properties to add dummy animation storyboards to the trigger
  • activating WPF animation tracing and filtering the results to only the entries with the dummy storyboards

Download sample app with source code



9 comments:

Timmy Kokke said...

Awesome! Thanks for sharing, just what I needed. Attached properties are usefull in a lot ways. I never thought of using them inside triggers.

Michael H said...

Perfect! Amazing! Thank you so much for sharing this with the community.

Amarnath said...

Superb.... You made my life easy

-Amar

Josh said...

Animation storyboards are a really cool and orignal way to achive this, I don't think I would have thought of that in a million years!

Thanks.

Christian Moser said...

Hi,

I created a tool called "WPF Inspector" that attaches to a running WPF application and shows all triggers with their conditions and actual states.

This is really helpful to debug triggers.

You can find it here: http://www.wpftutorial.net/inspector.html

Greetings
Christian

JasonDW said...

This is great, but it doesn't work on EventTriggers. I was trying to figure out why and haven't made any progress. It looks like you are attaching the TriggerBase so I would think it would work.

JasonDW said...

Oh... I see why it doesn't work on EventTriggers. It's because your dummy storyboard is associated with EnterActions and ExitActions of the trigger. EventTriggers don't have EnterActions and Exit actions. I've updated the code to check for eventtrigger type and to add the storyboard to the actions (rather the enteraction).

GO said...

@Christian: indeed your WPF Inspector is very handy for this as well as for heaps of other stuff! Thank you for sharing!

Anonymous said...

Brilliant, thank you.

I had two related but overlapping triggers and couldn't figure out why the second wasn't working until I used your class. Saved me a lot of headache.

Thank you!