Home » Debug WPF Design Time Errors

Debug WPF Design Time Errors

In one of my posts I told about WPF Visual Designer and how to display design time data in MVVM projects. I hope you had a chance to try it and work with this nice WPF feature because it is one of the most time-saving features in WPF development. If you still didn’t – I suggest you start using it immediately.

But like in other cases, when writing a code you can make an error where you initialize your design time data. When it happens you won’t be able to see you design time data and it can be very difficult to find where the problem is, because you can’t debug design data in a regular way. In this post we’ll talk about how to debug design time data.

In order to illustrate let’s take source code from my previous post and add to PhoneInfoViewModel property.

PropertyViewModel is a simple class with 2 string properties Label and Value

Let’s add some XAML in order to display our properties:

In order to see Properties in designer we should write some initialization code:

Refresh your designer and … All design time data disappeared, you have d:DataContext attribute underlined and “Object reference not set to an instance of an object” tooltip appears.  Now you have to understand where NullReferenceException thrown and fix it. It’s a bit complicated, because you are in design time and you can’t put a breakpoint and check where the exception comes from.

Design Time Error

So how to fix the bug?

First of all, you can debug design time data the same way as “normal” application, but you need to make several extra steps:

  1. Close all XAML documents in your solution.
  2. Kill all XDesProc.exe processes.
  3. Open MainWindow.xaml in Visual Studio

Open an additional instance of Visual Studio.

  1. Open the same solution in the debug Visual Studio instance.
  2. Put an breakpoint in MainWindow constructor.
  3. Attach to XDesProc.exe process (Debug-> Attach to process…) .

Now reopen MainWindow.xaml and your breakpoint will be active. Now you can debug you design time & to find out that you forgot to initialize _properties field.

NullReferenceException

Add _properties = new ObservableCollection<PropertyViewModel>() to your constructor to fix the bug.

That’s all. Now you can do incredible things and make you views work in design time, and if they don’t work, you can understand why and even fixing them.

P. S.

In Visual Studio 2010 you should attach to devenv.exe instead of XDesProc.exe in order to debug design time behavior.

One comment

  1. Vlad says:

    Thak you so much! This is GREAT! I spend many hours to that problem. Many thanks from Russia! =) I think this post must be as an answer on stackoverflow.com.

    P.S. Sorry for my bad english.

Leave a Reply

Your email address will not be published. Required fields are marked *