Aug 17, 2009

Selective Debugging for WPF Binding

Posted by rravuri


Have you ever pulled out your hair trying to figure out why / why not a particular value is (or not) being set on your control , what was wrong with the binding?

I have seen people suggest exotic solutions where in there used a special converter so that they can debug the value being bound, or turn-on WPF tracing which generates a huge volume of trace statements that you will have to filter, well i found a easier way to debug just the one binding that matters by specifying a  binding attribute.

   1: <UserControl

   2:     xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase">

   3: ...

   4: ...

   5: <Grid>

   6:   <TextBlock VerticalAlignment="Center" Text="{Binding Name,diag:PresentationTraceSources.TraceLevel=High}"/>

   7: </Grid>


this produces following output in your trace log

   1: System.Windows.Data Warning: 54 :   Path: 'Name'

   2: System.Windows.Data Warning: 56 : BindingExpression (hash=42296571): Default mode resolved to OneWay

   3: System.Windows.Data Warning: 57 : BindingExpression (hash=42296571): Default update trigger resolved to PropertyChanged

   4: System.Windows.Data Warning: 58 : BindingExpression (hash=42296571): Attach to System.Windows.Controls.TextBlock.Text (hash=56437836)

   5: System.Windows.Data Warning: 63 : BindingExpression (hash=42296571): Resolving source 

   6: System.Windows.Data Warning: 66 : BindingExpression (hash=42296571): Found data context element: TextBlock (hash=56437836) (OK)

   7: System.Windows.Data Warning: 74 : BindingExpression (hash=42296571): Activate with root item CPUPlugin (hash=28857250)

   8: System.Windows.Data Warning: 104 : BindingExpression (hash=42296571):   At level 0 - for CPUPlugin.Name found accessor ReflectPropertyDescriptor(Name)

   9: System.Windows.Data Warning: 100 : BindingExpression (hash=42296571): Replace item at level 0 with CPUPlugin (hash=28857250), using accessor ReflectPropertyDescriptor(Name)

  10: System.Windows.Data Warning: 97 : BindingExpression (hash=42296571): GetValue at level 0 from CPUPlugin (hash=28857250) using ReflectPropertyDescriptor(Name): 'CPU'

  11: System.Windows.Data Warning: 76 : BindingExpression (hash=42296571): TransferValue - got raw value 'CPU'

  12: System.Windows.Data Warning: 85 : BindingExpression (hash=42296571): TransferValue - using final value 'CPU'