Struan
Struan

Reputation: 655

Why does WPF databinding swallow exceptions?

I recently wasted a lot of time trying to debug a WPF datagrid (from the WPF Toolkit). I had a column bound to a linq query with a property that was throwing an exception (in a few rows). WPF seems to catch the exception and it just makes the cells blank. I have fixed the bug causing the exception, but I would like to change WPF's behavior. I always want to know if something is wrong. Why is swallowing the exception the default behavior and how can I change it?

Upvotes: 21

Views: 5050

Answers (5)

Benoit Blanchon
Benoit Blanchon

Reputation: 14571

I implemented a solution very similar to the one proposed by Karsten:

  1. Derived a TraceListener that throws instead of logging
  2. Added that listener to PresentationTraceSources.DataBindingSource

Please see the complete solution on GitHub, it includes a demo application and a unit test project.

Exception in Visual Studio

Upvotes: 0

Stephen Wrighton
Stephen Wrighton

Reputation: 37880

Databinding errors are swallowed natively, but they are displayed in the OUTPUT dialog in the visual studio interface. If you wish to be notified (and have as much control over it as possible), then you can use Trace Sources.

Bea Stollnitz provides a great writeup about this (and debuginng the bindings in general) here: http://www.beacosta.com/blog/?p=52

Apparently, this blog has departed, but we do have the WayBack Machine: http://web.archive.org/web/20091103200557/http://www.beacosta.com/blog/?p=52

I also added it to my Coding notebook for Evernote: https://www.evernote.com/pub/view/sswrighton/coding/f9786746-94c3-41b5-bcd2-c12992d331a6?locale=en#st=p&n=f9786746-94c3-41b5-bcd2-c12992d331a6

Upvotes: 12

Karsten
Karsten

Reputation: 8164

Here is my favorite. It works without changing the app.config

Upvotes: 7

Ana Betts
Ana Betts

Reputation: 74702

Enabling First Chance Exceptions in the debugger (Debug -> Exceptions) will help with this as well, though it can get annoying

Upvotes: -1

JoshKraker
JoshKraker

Reputation: 373

In case you would like to setup a validation for it, you can add ExceptionValidationRule

http://msdn.microsoft.com/en-us/library/system.windows.controls.exceptionvalidationrule.aspx

Upvotes: 1

Related Questions