More on debugging DependencyProperty

In the previous post on DependencyPropertyHelper, we explored one way of debugging the Dependency Properties.

The DepedencyPropertyHelper provides you details on from which value provider the final value was calculated from. However, if you want to trace and ensure the binding has been set correctly, you could make use of PresentationTraceSources.

For example, consider following binding.

<Button Content="{Binding ButtonTitle}"/>

To enable tracing, one could enable the PresentationTraceSources as,

<Button Content="{Binding ButtonTitle, diag:PresentationTraceSources.TraceLevel=High}"/>

Do not forget to add reference to System.Diagnostics

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

Now your output window would have the entire trace.

System.Windows.Data Warning: 60 : BindingExpression (hash=28316044): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=28316044): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=28316044): Attach to System.Windows.Controls.Button.Content (hash=17324607)
System.Windows.Data Warning: 67 : BindingExpression (hash=28316044): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=28316044): Found data context element: Button (hash=17324607) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=28316044): DataContext is null
System.Windows.Data Warning: 65 : BindingExpression (hash=28316044): Resolve source deferred
System.Windows.Data Warning: 67 : BindingExpression (hash=28316044): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=28316044): Found data context element: Button (hash=17324607) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=28316044): Activate with root item ShellViewModel (hash=18700393)
System.Windows.Data Warning: 108 : BindingExpression (hash=28316044):   At level 0 - for ShellViewModel.ButtonTitle found accessor RuntimePropertyInfo(ButtonTitle)
System.Windows.Data Warning: 104 : BindingExpression (hash=28316044): Replace item at level 0 with ShellViewModel (hash=18700393), using accessor RuntimePropertyInfo(ButtonTitle)
System.Windows.Data Warning: 101 : BindingExpression (hash=28316044): GetValue at level 0 from ShellViewModel (hash=18700393) using RuntimePropertyInfo(ButtonTitle): 'Proceed'
System.Windows.Data Warning: 80 : BindingExpression (hash=28316044): TransferValue - got raw value 'Proceed'
System.Windows.Data Warning: 89 : BindingExpression (hash=28316044): TransferValue - using final value 'Proceed'

You could also set write custom TraceListener to break at the binding error. For example,

PresentationTraceSources.Refresh();
PresentationTraceSources.DataBindingSource.Listeners.Add(new ConsoleTraceListener());
PresentationTraceSources.DataBindingSource.Listeners.Add(new DebugTraceListener());
PresentationTraceSources.DataBindingSource.Switch.Level = SourceLevels.Error;

Where DebugTraceListener is defined as

public class DebugTraceListener : TraceListener
{
    public override void Write(string message)
    {
    }

    public override void WriteLine(string message)
    {
        Debugger.Break();
    }
}

This would ensure a break point is hit whenever encountered with a missing binding.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s