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"
        <Setter Property="FontWeight" Value="Bold"/>

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


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


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!


Christian Moser said...


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:


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!

Anonymous said...

Download link is broken. Could you upload it again?

Rodrigo Basniak said...
This comment has been removed by the author.
Rodrigo Basniak said...

How would you use this in code, instead of xaml?

Rob van Daal said...

Indeed, the download link seems to be broken. But if you use the arrow downloadbutton at the right (don't click on the .zip file), then it works.