Question

I am new to WPF and I am trying to resolve an error. I am trying to build a custom control library where I can make my own control objects. When I go to File > New Project > WPF Custom Control Library > [Enter name] > Save, then instant error:

The name "CustomControl1" does not exist in the namespace "clr-namespace:ProjectName"

I did not edit any code but immediately an error. For reference, the error is inside Generic.xaml.

<ResourceDictionary
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:local="clr-namespace:ProjectName">
       <Style TargetType="{x:Type local:CustomControl1}">  //<--Fails here
           <Setter Property="Template">
               <Setter.Value>
                   <ControlTemplate TargetType="{x:Type local:CustomControl1}">  // Fails here as well
                       <Border Background="{TemplateBinding Background}"
                               BorderBrush="{TemplateBinding BorderBrush}"
                               BorderThickness="{TemplateBinding BorderThickness}">

                       </Border>
                   </ControlTemplate>
               </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>

I am using Visual Studio 12 and .NET 4. Any ideas?

Was it helpful?

Solution

This is an IntelliSense error, not a build error, so it should not affect building the project. To resolve this error, either

  • build the project, or
  • edit the document (e.g. by removing a > from a tag, then adding it back).

When you open the document, the XAML designer loads in the background to provide IntelliSense information. It loads information for unbuilt types (i.e., types defined in the current Solution but which have not yet been built into an assembly) asynchronously, and often this process completes after the designer has completed the initial parse of the document.

Building the project will cause the unbuilt types to be built (thus resolving the issue), and making a material edit to the document will cause the designer to reparse the document with the newly available type information (ideally, the document should be reparsed when the unbuilt type information becomes available).

OTHER TIPS

According to the answer of James McNellis, in my case I had to comment out the XAML-section causing the error (since the error did not allow to rebuild), then CLOSE the file itself, so its not open in VS, then I could make a successful build. Then I uncommented the XAML-section and VS was able to locate the local classes... In case, someone would stumble on this. Visual Studio 2012 Express for Windows Desktop... BR,

Daniel

This can also happens if you don't write the default style for CustomControl in generic.xaml. It generates something like this in generic.xaml:

<Style TargetType="{x:Type local:MyCustomControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyCustomControl}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

you can find this piece of code and change or remove it.

Assuming your assembly is called ProjectName, and your target namespace is also called ProjectName

You have to change the following:

xmlns:local="clr-namespace:ProjectName">

to

xmlns:local="clr-namespace:ProjectName;assembly=ProjectName">

Make sure that CustomControl1 doesn't have any errors in it and check your namespace.

Rebuilding and cleaning several times fixed the issue for me.

Modify the derived class in derivedclass.cs to Abstract:

public abstract class CustomControl : Control

And then use it in the .xaml files

<local:CustomControl>
...
</local:CustomControl>

//also see this topic: Inheriting from a UserControl in WPF

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top