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



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