59

I have an Outlook 2013 and 2016 VSTO Add-in project and am trying to add a WPF user control to a custom task pane as described here.

The problem I have is when I add the User Control (WPF) it generates me a WPF control with a grid, but automatically throws an error of "The type 'UserControl' does not support direct content".

WPF generated:

<UserControl x:Class="TestNamespace.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:TestNamespace"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>

</Grid>
</UserControl>

I know in the past I have had to add the WPF project type guid to the .proj file to get some things to work, but adding this made no difference (in fact it would not even load when in a certain order).

Original:

<ProjectTypeGuids>{BAA0C2D2-18E2-41B9-852F-F413020CAA33};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

Crashes:

<ProjectTypeGuids>{BAA0C2D2-18E2-41B9-852F-F413020CAA33};{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

Doesn't crash, but doesn't fix the error:

<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{BAA0C2D2-18E2-41B9-852F-F413020CAA33};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

Can anyone point me in the right direction?

UPDATE

I tried creating a new class library project straight out of the box, added a WPF user control, then added the reference to System.Xaml and I have the same issue.

NAJ
  • 1,175
  • 2
  • 12
  • 22

10 Answers10

167

For anyone who having this problem on Visual Studio 2015, try to add (if it's not already added) System.Xaml reference to your project. Visual Studio simply fails to show reference error.

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
alicanerdogan
  • 1,837
  • 1
  • 12
  • 11
105

Add System.Xaml and UIAutomationProvider references to your project, after that clear solution and then build again

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
paulgai
  • 1,051
  • 1
  • 7
  • 2
  • 7
    And then edit your XAML and the problem comes back... VS2015 Update 3 – Daniel Earwicker Oct 03 '16 at 14:48
  • Confirming in VS015. Just adding system.xaml made the problem disappear. not building. Adding a button caused the error to return. Adding a reference to UIAutomationProvider cleared the error. How can I/We fix the templates to include these references and make this a clean way to create new controls for the toolbox. – dr d b karron Jan 12 '17 at 11:39
  • Had a reference to System.Xaml but no reference to UIAutomationProvider. Adding this fixed same issue for me... This was a new project, VS 2015 14.0.25431.01 Update 3 under a new WPF User Control Library Project with .NET 4.6.2 referenced. Hope this also helps someone else. Thanks for the help. – Michael Puckett II Jan 14 '17 at 17:46
  • 5
    This should be the answer. VS 2015 needs both XAML and UIAutomationProvider. I would add that you need to restart VS after adding the references, otherwise the issue continues after editing. – Walter Feb 27 '17 at 13:56
  • @Walter thanks for the hint, restart really was necessary – Florian Koch Mar 28 '17 at 11:59
  • 2
    This problem has surfaced for me in VS2017 and none of the solutions work. – avenmore Mar 30 '17 at 16:32
  • 1
    I can confirm that this solution is also valid for VS2017. I did not even have to restart visual studio. – guerrillacodester Apr 24 '17 at 18:47
18

Add System.Xaml and UIAutomationProvider references, and then restart Visual Studio solve the problems.

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
SLdragon
  • 1,477
  • 16
  • 19
5

In VS2017 (15.3.5) this problem occurs if the base UserControl/Window of the UserControl your are editing is in the same library/exe. After starting VS it is fine for a few minutes, then something in the background hiccoughs and the entire XAML file is blue-squiggled. Compile and it goes away, start typing and it's instantly back. Intellisense still works, but it makes the XAML editor almost unusable.

The only way to fix it is to move base classes into another library.

avenmore
  • 2,809
  • 3
  • 33
  • 34
4

Just remove System.Xaml, then add it again.

Ahmed Alayat
  • 111
  • 2
  • 11
2

Try with exposing new Content property like the example and use ContentPropertyAttribute to the class. For me that helped. I had the problem in VS 2017.

[ContentProperty( "Content" )]
public class MyUserControl: UserControl
{
    public new Object Content
    {
        get => base.Content;
        set => base.Content = value;
    }
    ...
}
Monika Mateeva
  • 171
  • 1
  • 6
1

While missing references have been mentioned as a solution, I discovered that it can also be a case of needing to resolve class ambiguities in your references.

For me, the issue was caused by an external library that had defined its own ContentPropertyAttribute in the System.Windows.Markup namespace which was causing content attributes to fail completely. Removing the reference will fix the issue, but if that is not an option, then you will have to set up a namespace alias in the reference's properties instead.

Jason Lim
  • 171
  • 6
0

So it seems the coding faries have been in overnight as this now seems to work perfectly without me having changed anything, very odd, but at least I can carry on now!

NAJ
  • 1,175
  • 2
  • 12
  • 22
0

Beside adding already pointed out references, I had to close and reopen solution. If even this does not solve it, restart Visual Studio.

Julio Nobre
  • 4,196
  • 3
  • 46
  • 49
0

Additional help in VS2019:

To add reference go to menu Project > Add Project Reference... > Projects > Browse..

The System.Xaml reference can be found in the .Net framework folder. ex. C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Xaml.dll

and restarting VS required.

trydent
  • 51
  • 2