I learnt about problems with nested templates in WPF with .NET 4

We had a very subtle problem in one of the projects I was working on. The application was falling over with a null reference exception inside something called IStyleConnector.Connect. This is part of the core framework.

The code in the application where this occurred had been around for a while and this had only just started happening. After a bit of googling I came across the following article:

http://support.microsoft.com/kb/2464222 - "FIX: A NullReferenceException exception occurs when you run a .NET Framework 4.0-based WPF application that has events in a nested template"

It looks like in upgrading from .NET 3.5 to 4.0 we had hit this issue. There appears to be an issue where there is an event handler inside nested templates where it looks at the wrong template.

There is a hotfix but that wasn't an option as we don't have control over the installs on client machines.

The search for a solution to this problem lead to another page ...
https://connect.microsoft.com/VisualStudio/feedback/details/560631/net-framework-4-0-regression-in-wpf-exception-in-istyleconnector-connect-with-nested-itemscontrols-and-event-style-attributes-in-datatemplate

In the comments this has discussions about potential side-effects from the previously mentioned hotfix so even if we had control it is not a route we would probably have taken.

However this article also has two suggested workarounds. The first is to use a dynamic resource and the second is to move the nested template out into its own resource. We used the latter approach and this fixed our problem. Happy days!


1 comments:

Thanks for the post and the link! As suggested by Microsoft, I changed "StaticResource" to "DynamicResource" and it solved my problem.

Reply