Click here to Skip to main content
15,885,537 members
Articles / Desktop Programming / WPF
Tip/Trick

Don’t Move or I Will Shoot Your Bindings

Rate me:
Please Sign up or sign in to vote.
4.27/5 (3 votes)
29 Mar 2011CPOL1 min read 12.1K   4   2
How WPF binding systems sometimes fail silently.

WPF bindings and MVVM are nice when they work, but can become a royal pain when they don’t. This is especially annoying when you create bindings in code, because then you don’t get the automatic tracing which is enabled for bindings in XAML. Any error is silently swallowed. Here’s what I found out (waiting to confirm it with MSDN documentation and independent small tests):



  • if you have a one-way binding and someone changes the target value not using the binding, the binding will be deactivated.
  • if you have a two-way binding, someone changes the target value not using the binding, and something goes wrong updating the source (e.g., converter’s ConvertBack() method throws an exception), your binding will be deactivated.

Deactivated bindings look like they are there, but they don’t do anything. Effectively, binding stops working, and it is often very difficult to know exactly why. If you created the binding in code, you can look at the status of the binding expression. If you created the binding in XAML, getting to its status is much more difficult. Also, it is not possible (to the best of my knowledge) to put a breakpoint when binding status changes: there is no data breakpoints in managed code, and I could not get .NET source debugging to work for WPF bindings for whatever reason. I could see things like DependencyObject, but not BindingOperations.


Bottom line: binding systems sometimes fail silently. Detecting and debugging those failures can be very time consuming and difficult.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Thomson Reuters
United States United States
Ivan is a hands-on software architect/technical lead working for Thomson Reuters in the New York City area. At present I am mostly building complex multi-threaded WPF application for the financial sector, but I am also interested in cloud computing, web development, mobile development, etc.

Please visit my web site: www.ikriv.com.

Comments and Discussions

 
GeneralAny other possibilities for Binding deactivation in WPF, apart mentioned 2 possibilities? Pin
Harish Arumalla13-Mar-15 2:45
Harish Arumalla13-Mar-15 2:45 
GeneralReason for my vote of 5 Interesting findings regarding bindi... Pin
DrABELL29-Mar-11 8:52
DrABELL29-Mar-11 8:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.