Search code examples
wpfdesignerwinforms-interop

WinForms Designer fails when hosting nested WPF UserControls


I'm using VS2008 SP1 with the WPFToolKit v3.5.50211.1.

I have nested WPF UserControls in a Class Library project as follows:

  • MainWpfUserControl
  • Contains an instance of NestedUserControl
  • Which contains an instance of a DataGrid from the WPF Toolkit.

The same Class Library project contains a WinForms UserControl with an ElementHost that hosts MainWpfUserControl.

I've been getting strange errors when opening the WinForms UserControl in the designer. After a bit of digging around (catching and logging the exception thrown in the NestedUserControl constructor), it seems that an exception is thrown as follows:

[A]Microsoft.Windows.Controls.DataGrid cannot be cast to [B]Microsoft.Windows.Controls.DataGrid. Type A originates from 'WPFToolkit, Version=3.5.40128.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'LoadFrom' at location 'C:\Program Files\WPF Toolkit\v3.5.50211.1\WPFToolkit.dll'. Type B originates from 'WPFToolkit, Version=3.5.40128.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'LoadNeither' at location 'C:\Documents and Settings\...\Local Settings\Application Data\Microsoft\VisualStudio\9.0\ProjectAssemblies\zfh65-u101\WPFToolkit.dll'

Has anyone experienced anything similar?

UPDATE

I seem to have successfully worked around the problem by putting WPFToolkit.dll in the GAC. But that's undesirable for other reasons (e.g. CopyLocal now defaults to false). So it seems the problem is related to Load Context. Is there any way to control this?


Solution

  • After a bit of research, the problems I'm having seem to be call caused by the VS designer loading the wrong version of an assembly. Looks similar to the issues described here:

    http://blogs.telerik.com/blogs/posts/08-12-23/visual_studio_ldquo_error_creating_control_rdquo.aspx

    http://connect.microsoft.com/VisualStudio/feedback/details/361826/error-creating-control-text-could-not-be-set-on-property-with-custom-asp-net-server-controls-in-vs-2008-sp1

    I can work around the problem by doing the following:

    • Close Visual Studio 2008

    • Clear the Assembly download cache (gacutil /cdl)

    • Delete the VS2008 ProjectAssemblies folder:

      %USERPROFILE%\Local Settings\Application Data\Microsoft\VisualStudio\9.0\ProjectAssemblies

    • Restart Visual Studio