Jump to content

Debugging WPF Bindings

Posted on:February 26, 2016 at 01:00 AM

WPF + MVVM is a very common way to make windows apps. It introduces the concept of data binding. We create a view-model based on one or many models and bind it to the view. Usually, it is easy… But how do you debug bindings if something is not going right? For example, you have Control Templates and styles and so on introduced in your app.

Since WPF 3.5 there is new diagnostics featured that you can turn on and it will output detailed binding messages to the Output window. Immensely helpful when you are trying to figure out why your binding does not work or works incorrectly.

To set it up, first add diagnostics to your namespace in XAML file:

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

Then enable it on per-binding instance as such:

 ItemsSource="{Binding Path=PaneItems, diag:PresentationTraceSources.TraceLevel=High} 

Now when you run your app watch Output window for clues on why binding is failing.

Hope this helps save your sanity while debugging bindings as much as it does mine :-)