Skip Navigation LinksHome > View Post

New in WPF 3.5: 3. Enhanced Debugging Features

Debugging DataBinding in WPF has always been a tricky business. There's a number of popular techniques people use such as implementing an IValueConverter that doesn't do anything but does allow the developer to drop a breakpoint in the guts of the databinding. Keeping a keen eye on the output window in Visual Studio is the recommended first port of call but it's output isn't exactly verbose, until now...

Yes, with WPF 3.5 you can configure a binding to spew out debug content and set the volume level to one of four settings: None, Low, Medium and High. These settings are afforded by the PresentationTraceLevel enumeration residing in the System.Diagnostics namespace of the WindowsBase assembly (not the System.dll where most of the System.Diagnostics bits live).

Firstly, we should register the clr namespace with an xml namespace:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase">

And then apply the PresentationTraceLevel to our binding

<TextBox>
<TextBox.Text>
<Binding Path="PersonName"
diag:PresentationTraceSources.TraceLevel="High"/>
</TextBox.Text>
</TextBox>

Note that you have to use the verbose form of Binding as there's no way of applying this to a Binding Markup Extension (that I'm aware of). Here's an example of some High TraceLevel output.

System.Windows.Data Warning: 48 : Created BindingExpression (hash=4322080) for Binding (hash=40953208) within MultiBindingExpression (hash=50800521)
System.Windows.Data Warning: 49 : Path: 'NormalizedX'
System.Windows.Data Warning: 51 : BindingExpression (hash=4322080): Default mode resolved to OneWay
System.Windows.Data Warning: 52 : BindingExpression (hash=4322080): Default update trigger resolved to Explicit
System.Windows.Data Warning: 53 : BindingExpression (hash=4322080): Attach to System.Windows.Media.TranslateTransform.X (hash=60275915)
System.Windows.Data Warning: 55 : BindingExpression (hash=4322080): Use Framework mentor <null>
System.Windows.Data Warning: 58 : BindingExpression (hash=4322080): Resolving source
System.Windows.Data Warning: 60 : BindingExpression (hash=4322080): Framework mentor not found
System.Windows.Data Warning: 56 : BindingExpression (hash=4322080): Resolve source deferred
System.Windows.Data Warning: 85 : BindingExpression (hash=4322080): Got InheritanceContextChanged event from TranslateTransform (hash=60275915)
System.Windows.Data Warning: 85 : BindingExpression (hash=4322080): Got InheritanceContextChanged event from TranslateTransform (hash=60275915)
System.Windows.Data Warning: 85 : BindingExpression (hash=4322080): Got InheritanceContextChanged event from TranslateTransform (hash=60275915)
System.Windows.Data Warning: 48 : Created BindingExpression (hash=1847745) for Binding (hash=18094558) within MultiBindingExpression (hash=41061)
System.Windows.Data Warning: 49 : Path: 'NormalizedX'
System.Windows.Data Warning: 51 : BindingExpression (hash=1847745): Default mode resolved to OneWay
System.Windows.Data Warning: 52 : BindingExpression (hash=1847745): Default update trigger resolved to Explicit
System.Windows.Data Warning: 53 : BindingExpression (hash=1847745): Attach to System.Windows.Media.TranslateTransform.X (hash=16039693)
System.Windows.Data Warning: 55 : BindingExpression (hash=1847745): Use Framework mentor <null>
System.Windows.Data Warning: 58 : BindingExpression (hash=1847745): Resolving source
System.Windows.Data Warning: 60 : BindingExpression (hash=1847745): Framework mentor not found
System.Windows.Data Warning: 56 : BindingExpression (hash=1847745): Resolve source deferred
System.Windows.Data Warning: 85 : BindingExpression (hash=1847745): Got InheritanceContextChanged event from TranslateTransform (hash=16039693)
System.Windows.Data Warning: 85 : BindingExpression (hash=1847745): Got InheritanceContextChanged event from TranslateTransform (hash=16039693)
System.Windows.Data Warning: 85 : BindingExpression (hash=1847745): Got InheritanceContextChanged event from TranslateTransform (hash=16039693)
System.Windows.Data Warning: 58 : BindingExpression (hash=4322080): Resolving source
System.Windows.Data Warning: 61 : BindingExpression (hash=4322080): Found data context element: Ellipse (hash=45943265) (OK)
System.Windows.Data Warning: 69 : BindingExpression (hash=4322080): Activate with root item Marker (hash=34684851)
System.Windows.Data Warning: 98 : BindingExpression (hash=4322080): At level 0 - for Marker.NormalizedX found accessor ReflectPropertyDescriptor(NormalizedX)
System.Windows.Data Warning: 94 : BindingExpression (hash=4322080): Replace item at level 0 with Marker (hash=34684851), using accessor ReflectPropertyDescriptor(NormalizedX)
System.Windows.Data Warning: 91 : BindingExpression (hash=4322080): GetValue at level 0 from Marker (hash=34684851) using ReflectPropertyDescriptor(NormalizedX): '0.779073500892602'
System.Windows.Data Warning: 71 : BindingExpression (hash=4322080): TransferValue - got raw value '0.779073500892602'
System.Windows.Data Warning: 78 : BindingExpression (hash=4322080): TransferValue - using final value '0.779073500892602'
System.Windows.Data Warning: 58 : BindingExpression (hash=1847745): Resolving source
System.Windows.Data Warning: 61 : BindingExpression (hash=1847745): Found data context element: Ellipse (hash=54181020) (OK)
System.Windows.Data Warning: 69 : BindingExpression (hash=1847745): Activate with root item Marker (hash=63037041)
System.Windows.Data Warning: 98 : BindingExpression (hash=1847745): At level 0 - for Marker.NormalizedX found accessor ReflectPropertyDescriptor(NormalizedX)
System.Windows.Data Warning: 94 : BindingExpression (hash=1847745): Replace item at level 0 with Marker (hash=63037041), using accessor ReflectPropertyDescriptor(NormalizedX)
System.Windows.Data Warning: 91 : BindingExpression (hash=1847745): GetValue at level 0 from Marker (hash=63037041) using ReflectPropertyDescriptor(NormalizedX): '0.04355085213775'
System.Windows.Data Warning: 71 : BindingExpression (hash=1847745): TransferValue - got raw value '0.04355085213775'
System.Windows.Data Warning: 78 : BindingExpression (hash=1847745): TransferValue - using final value '0.04355085213775'

WPF 3.5 - Giving good debug to the masses. Read more about debugging databinding in Beatriz Costa's post on How can I debug WPF bindings?.

Tags: WPF

 
Josh Post By Josh Twist
11:24 AM
17 Feb 2008

» Next Post: Different DataTemplates for different types
« Previous Post: Using the Source Code Editor for XAML in Visual Studio

Comments are closed for this post.

Posted by Simon @ 12 May 2009 2:08 AM
No you don't have to use verbose binding syntax:

<TextBox Text="{Binding PersonName, diag:PresentationTraceSources.TraceLevel=High}" />

Posted by Josh @ 12 May 2009 7:52 AM
Well I never, I'm ashamed I never tried that to be honest. Had no idea you could use attached properties on MarkupExtensions - makes sense though :)

Happy days - thanks.

© 2005 - 2014 Josh Twist - All Rights Reserved.