Click here to Skip to main content
15,394,299 members
Articles / Desktop Programming / WPF
Posted 22 Sep 2010


48 bookmarked

A Guide to Cleaner XAML with Custom Namespaces and Prefixes (WPF/Silverlight)

Rate me:
Please Sign up or sign in to vote.
4.76/5 (35 votes)
22 Sep 2010Ms-PL4 min read
Clean up and manage your XAML references. This can be done using custom namespaces (scheme URLs) and custom prefixes.


When working with any type of application, it's a good idea to split up your solution in multiple projects (assemblies) and namespaces. As your project grows, you'll see your amount of namespaces grow with it. And if you plan to use classes / controls from these namespaces in your XAML code, you'll have to declare them with the following syntax:


An example could be:


If you are referencing an other assembly, you'll also need to add the assembly name:


For example:


Some namespaces are added by default when you add a new XAML control in your project (be it a UserControl or a Page).
This is how it should look like:

<UserControl x:Class="Sandworks.Silverlight.NamespaceExample.MainPage" 

But, as stated before, when your project starts growing and you need to work with different namespaces and assemblies, your references might start piling up and your XAML code gets bloated.

Real Life Example

In the code, you'll find the following projects:

These projects reflect how a real application could look like. All controls are organised in a separate namespace Controls (could even be another assembly) and the same thing applies to the Converters. Then you could have another assembly with more converters. Maybe this assembly contains some global converters that are used throughout all the projects in your company.

Looking at the XAML of MainPage, you'll notice the following:

<UserControl x:Class="Sandworks.Silverlight.NamespaceExample.MainPage"

For this small project, I already have 3 declarations. Imagine 5 more assemblies, and 20 UserControls making use of all these assemblies. You'll end up copy/pasting, duplicating code, …

But what are our options to clean up our XAML?

Option 1: Custom Namespaces

In the example project, I added a Calendar (not a default control) to the MainPage. You'll see that Visual Studio will add a reference to System.Windows.Controls and will also add the following to your MainPage:

Notice the following:

  1. Instead of adding "my" or any other word as prefix, it added sdk. It would be nice if we can trigger this behavior when adding our controls.
  2. Instead of a clr-namespace + assembly reference, it adds a regular (schema) URL.

To achieve this behavior, you need to follow the next steps:

  1. Go to the project where you want to map CLR namespaces to a custom namespace
  2. Open AssemblyInfo.cs under Properties
  3. Add the following to the top of your code
    using System.Windows.Markup;
  4. Add the XmlnsPrefix and XmlnsDefinition attributes.


[assembly: XmlnsPrefix("", "sw")]
[assembly: XmlnsDefinition("", 
[assembly: XmlnsDefinition("", 

As you can see, we use the XmlnsDefinition attribute to map multiple CLR namespaces to a single custom namespace. The beauty of this is that you aren't limited to a single assembly. In the downloadable source code, you'll see these definitions for both the application as for the class library. You could think of organising all your controls, converters, utils, … under a project wide namespace.

So, before we had this:


And now we have this:


Again, imagine 5 more assemblies, and 20 UserControls making use of all these assemblies. No bloating!
If you're referencing 1 or 500 assemblies, you'll just be using this single custom namespace.

Oh, and if you didn't notice yet, the XmlnsPrefix declares the default prefix (for that namespace) that should be added automatically. When I added the Calendar, we automatically got an sdk prefix, and now for my own controls I'll automatically have the sw prefix.

Option 2: No Namespaces

Now you don't have to stop at Option 1. You could actually get rid of all your extra namespaces. As stated before, you are not limited to a single assembly. Meaning more than one assembly can declare the same namespace.

If you take a look at a default UserControl, you'll always see this line:

<UserControl x:Class="Sandworks.Silverlight.NoNamespaceExample.MainPage"

Well, now you could map all your CLR namespace to this custom namespace, as follows:

[assembly: XmlnsDefinition("", 
[assembly: XmlnsDefinition("", 

Now you achieved 2 things:

  • You do not need to add extra namespace references in your XAML controls.
  • You get rid of prefixes. Since the presentation namespace is declared without prefix, all your controls, converters, … are accessible without prefix. And this is great for using Intellisense:

I myself prefer Option 1. Totally removing namespaces and prefixes could also become a problem in larger projects. The thing I tend to do is create 1 custom namespace for company wide assemblies and 1 custom namespace for project specific assemblies/projects.


The source contains 2 scenarios. One with regular namespace usage and one with our custom namespaces.
Download here:

More information on this topic can be found at



  • 22nd September, 2010: Initial post


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

Sandrino Di Mattia
Technical Lead RealDolmen
Belgium Belgium
I'm a Technical Consultant at RealDolmen, one of the largest players on the Belgian IT market:

All posts also appear on my blogs: and

Comments and Discussions

QuestionReaders beware Pin
Mike Nakis25-Aug-20 0:28
MemberMike Nakis25-Aug-20 0:28 
QuestionMy vote for 5 Pin
causg6-May-13 4:09
Membercausg6-May-13 4:09 
GeneralMy vote of 5 Pin
Sperneder Patrick3-Jan-13 19:03
professionalSperneder Patrick3-Jan-13 19:03 
GeneralMy vote of 5 Pin
Member 76546417-Jun-12 4:24
MemberMember 76546417-Jun-12 4:24 
GeneralMy vote of 5 Pin
Chris Grove28-Sep-10 0:28
MemberChris Grove28-Sep-10 0:28 
GeneralCrashes VS2010 Pin
terryterryd27-Sep-10 21:47
Memberterryterryd27-Sep-10 21:47 
GeneralRe: works here Pin
eiiv14-Oct-11 2:44
Membereiiv14-Oct-11 2:44 
GeneralMy vote of 5 Pin
p.e.rovsing27-Sep-10 10:47
professionalp.e.rovsing27-Sep-10 10:47 
GeneralMy vote of 5 Pin
FaReSs24-Sep-10 0:43
MemberFaReSs24-Sep-10 0:43 
GeneralExcellent Pin
FaReSs24-Sep-10 0:43
MemberFaReSs24-Sep-10 0:43 
QuestionBrilliant! Can you change the VS default? Pin
bowlermonk22-Sep-10 5:58
Memberbowlermonk22-Sep-10 5:58 
AnswerRe: Brilliant! Can you change the VS default? Pin
Sandrino Di Mattia22-Sep-10 9:05
MemberSandrino Di Mattia22-Sep-10 9:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.