Is there any way to effectively debug WPF data bindings?

add a dummy converter

<local:DebuggerConverter x:Key="DebuggerConverter" />

<TextBlock Text={Binding ToSomething, Converter={StaticResource DebuggerConverter}} />

the converter

public class DebuggerConverter : IValueConverter
{
  #region IValueConverter Members

  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    // Set breakpoint here
    return value;
  }

  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    // Set breakpoint here
    return value;
  }

  #endregion
}

or use this and look at your output window

<Window xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase">
  <TextBlock Text="{Binding ToSomething, diagnostics:PresentationTraceSources.TraceLevel=High}" />
</Window>

hope this helps


When i started with WPF, this is the major issue which i faced to debug my bindings. Everytime looking at Output window and searching for your binding seems cumbersome for very big applications. Then i came across this excellent article with detailed understanding of Bindings:

(original, dead link) http://bea.stollnitz.com/blog/index.php?s=presentationtrace

Archive: https://github.com/bstollnitz/old-wpf-blog/tree/master/45-DebuggingDataBinding

Hopefully, this might help you too... :)


I have no idea if VS provides an easy way to debug bindings, but I usually use Snoop for debugging bindings

It's a tool that will go through a WPF application and give you a TreeView of the application's Visual Tree. You can select an element to view its DataContext and other properties. If there are any binding errors, the property is usually highlighted and it will say what the error was. If the binding is failing because the property doesn't exist, I can usually trace the DataContext and figure out where I went wrong in my binding.

Tags:

Wpf