Gotchas: Debugging WPF Data Binding
Thursday, December 18, 2008 – 8:03 AMI’ve been playing around with WPF data binding recently and was struggling a bit with how to debug it when things weren’t working right. Translation: cursing a lot when everything was hawked and I couldn’t figure out why.
Suppose I have the following binding that doesn’t work the way I expect.
<TextBox Height="23" Name="textBox3" Width="52" Text="{Binding Path=Radius}" ToolTip="Initial radius." />
What I’d like is some debug output from WPF to tell me what I messed up.
Turns out that .NET 3.5 has a new TraceLevel attached property that can really help out. The fact that this is gets a tiny mention at the very end of the data binding overview article on MSDN is a mystery but there you go. Simply set this as part of the binding configuration. Don’t forget to include the diagnostics namespace in the root XAML element.
<UserControl x:Class="NBody.Viewer.Modules.Configurator.ConfiguratorView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase" Height="86" Width="400"> ...
<TextBox Height="23" Name="textBox3" Width="52" Text="{Binding Path=Radius, diagnostics:PresentationTraceSources.TraceLevel=High}" ToolTip="Initial radius." />
Easy! The debug window will now contain output like this:
System.Windows.Data Warning: 56 : BindingExpression (hash=30474330): Default mode resolved to TwoWay System.Windows.Data Warning: 63 : BindingExpression (hash=30474330): Resolving source
Here’s another post by Beatriz Stollnitz on some other debugging strategies. The configuration in the Trace Sources section didn’t work for me and I haven’t had time fit it but the article does include some other ideas.
One Response to “Gotchas: Debugging WPF Data Binding”
Here’s a useful technique for debugging triggers effectively:
http://www.wpfmentor.com/2009/01/how-to-debug-triggers-using-trigger.html
By Dan Lamping on Jan 23, 2009