WPF has a great databinding support but when it does not work it can be hard to debug. Understanding of databinding model in WPF can go a long way in making data binding easy. Since working on WPF i myself have faced "binding not working" syndrome. The general tendency then is to revert back to the code behind approach which does not look clean at all. In all cases its our lack of understanding of binding model or incorrect usage of binding the main culprit. In this post i would try to create a checklist which can be used to systematically debug any specific binding issue. But before reading this i would highly recommend that every WPF developer should thoroughly try to understand the WPF binding by going over the MSDN documentation and other sources. The steps mentioned here would make more sense if we have a clear understanding of databinding. We would be tacking the most common situation where a frontend UI is bounded to a backing entity.

  • Is the binding setup correctly (Binding is failing) : Misspelled names, incorrect DataContext can be first culprit for binding not working. This can be determined by looking at the output window while debugging the application. Each failing binding would be logged in the output window. Remember databinding are reevaluated anytime DataContext changes. If you are setting up DataContext in code, look for binding failure just after DataContext is assigned. Some of the most common reason for binding failures are
    • Misspelled member variable names. Very easy to detect
    • Incorrectly declare a Public property as Public field (no get;set;). AFAIK databinding works only on public properties
    • Declaring member as internal or with more restricted accessors.
    • Using incorrect DataContext. DataContext assigned at parent control level can directly affect the DataContext of a child control if child is not bounded to a specific DataContext explicitly. Therefore any parent DataContext change can invalidate the child control binding.
  • Binding not failing but data not getting updated: There can be numerous reasons for this. You may not see any binding failures in the output window specific to your binding but neither the source or target are getting updated. Some of the most common reason for this is
    • Binding mode may be incorrect. Check documentation for binding mode and use a appropriate binding accordingly.
    • Check for UpdateSourceTrigger Property of binding is setup correctly.
    • Value being assigned from source to target or vice versa is incompatible. You can use a debugging converter to see what values are being passed from source to target. More details about this are available on this blog entry.
    • Does the binding contain any validation rules. In case binding contains validation rules, any update on target( mostly UI element) will reevaluate the rule and if the rule fails source(entity) would not be update.
    • Incorrect DataContext assignment. This is a little difficult to find. Make sure that the DataContext used in databinding is pointing to the correct entity (or source). You may be expecting a different source to be updated and you have done databinding to a different source. DataContext assignments in code should be thoroughly evaluated.

Hope this list may help everyone debug their databinding issues. Some the stuff here is derived from an excellent blog entry by Bea Stollnitz

Thanks,
Chandermani