WPF Data Binding Troubleshooting Tips

December 10, 2011 05:05 by wjchristenson2

This week I ran into a WPF data binding problem and spent about a day trying to figure out why it wasn’t working.  A day is a long time to spend on why a “Text” property is not binding to an object’s property which is of the type “String”.  Here’s some quick tips on identifying the source of the data binding problem.

1)  Ensure Data Binding Debugging Information shows in Visual Studio’s Output window.  Visual Studio has the capability of showing verbose data binding trace information.  This can help you get to the bottom of the issue quickly.  In my case, my output window was not showing any data binding information…  You’ll need to adjust some Visual Studio debugging options.  First, ensure that all debugging information is not being sent to the immediate window.  Tools >> Options >> Debugging >> “Redirect all Output Window text to the Immediate Window”.  Uncheck that.  Next, Tools >> Options >> Debugging >> Output Window >> WPF Trace Settings >> Data Binding.  Select an option here.  When I was debugging I wanted to see everything going on so I selected “Verbose”.  Unfortunately, the information did not tell my anything as to why my two-way binding was not updating the underlying object’s property…

 

2)  Understand Dependency Properties and the INotifyPropertyChanged interface.  In order for the UI to properly show object property changes or to perform two-way binding, the property which it binds to needs to be a dependency property or the object needs to implement the INotifyPropertyChanged interface so that the UI properly reflects the object’s property changes and the UI can also update the object’s properties (if two-way binding is enabled).  In my case, my UI control was bound to a dependency property which its type also implemented INotifyPropertyChanged and my two-way binding still didn’t work…

 

3)  Are you using a Converter in the data binding expression?  Sometimes converters can throw exceptions and break the binding.  Put breakpoints in your converter and ensure the conversions are not throwing exceptions and breakpoints are being hit.  In my case, the convert from was being hit, but the convert back (updating the object from the UI via two-way data binding) was not being hit.  So one-way was working, but two-way was not…

 

4)  Are you binding a Custom Control which overrides/overloads the property which you are wanting to bind to?  This was the problem for me.  I was wanting to two-way bind the “Text” property of a custom user control to my object’s property.  The custom control was inheriting from TextBox and they were overloading the “Text” property.  Apparently this broke the two-way data binding.  I figured it out by simply dropping a vanilla TextBox onto my UI and binding it’s “Text” property to my expression and two-way binding worked great.  So, when things get really hairy, simplify what you are doing and add more and more things back into the picture until something breaks.  Troubleshooting 101 for the win.

Bookmark and Share