6. Data Binding Errors
By design, data binding errors do not cause exceptions. This behavior
is desirable because the source of a data binding may enter a valid and
invalid state a lot during the life of your application. Let’s take the
example we saw earlier where we have a list of controls bound to the SelectedItem
of a ListBox
. When there is no selection in the ListBox
the data binding is failing. Throwing an exception in that case would
be the wrong thing to do. So, as a developer, you will need a way to
actually see data binding failures. Luckily you can see them pretty
clearly in the Visual Studio Output window. When running your
application, you can use the View menu to show the Output window, as
shown in Figure 3.
Figure 3. Output window
When data binding fails, it adds a debug message to the Output window. For example, if you used the wrong path in a Binding
(e.g., Title
instead of Name
) you could see this in the Output window, as shown in Figure 4.
Figure 4. Binding error shown in the Output window
Bad paths aren’t the only binding errors that show up in the Output
window; bad conversions (really, any exceptions) do as well. For
example, if a user attempted to enter a bad date (e.g., 2/31/2010) into a
date field, the Output window would show that error, too, during data
binding (as seen in Figure 5).
Figure 5. Conversion error shown in the Output window