Infosys Microsoft Alliance and Solutions blog

« BizTalk 2006 Handbook and other books | Main | Adding Event handlers for controls in WPF (XAML) applications »

Using User Control in Windows WPF (XAML) Application

User controls are an essential part of writing Windows/Web applications. The other day I was trying to get a WPF user control (XAML) added to my WPF Window and was running into issues.

I am working on RTM Windows Vista Ultimate with VS2005 SP1 (with patch for Vista), .net fwk 3.0 RTM and November 2006 CTP of WPF extensions for VS. The scenario is as below.

I created a new WPF based Windows application and in the project also added a WPF User Control. I then tried to use this user control in the main application window. For doing this, one needs to first add the required namespace directive as below, to the root Window tag.


The namespace qualifier can be anything that you want to use and I have used "my" here. This is then prefixed while using the user control. In the main XAML there will be statement like the following

<my:UserControl1 ....></my:UserControl1>

Unfortunately,  when you do this, the VS designer goes for a toss and doesn't renders the UI anymore. There is some issue in correctly identifying and working with the current assembly. The solution, however will build and run fine. Just that you loose the designer support.

I tried by moving the user control to a different project, adding reference to that project and then adding the assembly name the namespace tag, but that also didn't work. Though not a serious limitation, since the code does runs fine, but it will definitely hamper the page design since you can no longer view it.

I decided to give this a shot with the Orcas March 2007 CTP. Though there are a few issues in working with XAML, like automatic insertion of quotes for property values, automatic insertion of closing tag, when the opening tag is closed, doesn't work, but the user control issue is definitely resolved and the designer works properly. Adding the requisite namespace is also simpler due to intellisense support as shown in the figure below. userControlXAML.jpg

After this you can go ahead and add the necessary user control directive as something like below and all is well.

<my:UserControl1 Height="100" Width="100"></my:UserControl1>

Incidently, as soon as you type <my: intellisense will show the various controls available in this namespace for use and you will see UserControl1 also listed there.

BTW, the feature of not adding the closing tag automatically is actually good, since in earlier versions, if you are trying to add a container control tag, you need to yourself cut and the closing tag and paste where you need it. However with Orcas, you can add the opening tag, and then scroll down to where you want to insert the closing tag and type </ and intellisense will show the right options for closing tags.


I have a quesion about usercontrol. In my control, I have one button and label. How can I create different controls in window f.e:
(I know that I can't do this in this method...) Is there any way to get into Label in my usercontrol from window?

@Peter, I didn't completely follow your question? If you are using VS 2005, after you add a new User Control (via the Add New Item), you can then drag and drop as many controls inside this user controls as you like. It is like designing for a regular windows or winform application's user control. Just that the UI is captured in XAML

problem solved:) solution: in my xaml view I had no grid, or any stackPanel... so I cant create more than one type of control.:)

@Peter, you are right. Note that in root windows or user control XAML node, the values inside the opening closing nodes are treated as assigned to the Content property and it can't be assigned to more than once. Hence you need to have a container to be able to create additional controls. VS in fact, thus provides a default Grid when you create a new window or user control

I have a related query.
I have developed a WPF UserControl named UC1, which I want to use in my WPF windowsApplication. Development tools for WinFx is installed on my Windows XP SP2 machine,

I am not getting the name of my usercontrol when I type


Why is that so. What I should be doing to solve this problem?

[The user control and the windows application are in the same solution, the user control is present in the application's output directory itself. Reference to the UserControl library is also added to the windowsapplication project ]

SMS: The intellisense that i talked works for Orcas.

Also note that when using the xmlns:my syntax to introduce a namespace, you don't directly refer to the user control, but to the name space it belogs to. If this is coming from a different assembly, you need to refer to the assembly as well. Check the documentation for more details.

I have developed a User Control which allows the user to Rotate and Move the control during runtime.
I have used ResourceDictionary to generate the Styles and Runtime behaviour.
I have built the usercontrol into a Dll and included as a reference to another project. I can see that the control loads perfectly on the UI with the design applied to it, but when i Run the project during runtime the user control design disappears.
I dont know where the condition fails as I am not able to debug the ResourceDictionalry during runtime. Can you help?

Were you able to find out a way around to get the UI designer working after placing the usercontrol.

I tried in the latest versions of vs 2008. You can use and do a lot of stuff with usercontrol but when it is placed inside a wpf window, designer seems not to work.

I no longer work with VS 2005 and WPF extensions, but now directly with .net 3.5 and VS 2008 and everything works beautifully.

User control should load normally in VS 2008. If it isn't, it is more due to either some issue in coding or some code executing that shouldn't when in design mode.

Check here for some designer loading issues

Thanks for the info! Very helpful in making the transition from 2.0 to 3.5.

I am developing a user control using WPF and I need to dynamically do some changes to the XAML code when my control is dropped on the window at design time.
I am able to make changes inside the code using OnInitialized event handler of my component but the changes are not reflected in the main window xaml file.
So, does anyone know how i can change the main window xaml file dynamically?

Thomas, I am not sure i completely understand what you are trying to ask. Can you elaborate a bit further or post a sample that shows what you are trying to do?

Sorry that my first post was vague.
My aim is to dynamically add a custom control at design time. For example, I want to ask the user at design time what controls he wants to add, then the controls are added automatically on the main design panel.
From what I have read till now, I think I need to get the ModelItem representing the main window and add other ModelItems to its children collection. That way the modelItems added will automatically appear in the design and xaml panels (during design time) of ms vstudio.
So, I am not sure yet how to get the ModelItem for the window that the user wants to add the custom controls to.
Sorry if I am not clear enough, I am new to WPF and trying to grasp as much as possible.
Thank you for your time.

When you say you want to ask the user at design about control's they want to add, is this some sort of Add-in you are building for VS? Will this also work for Expression Blend? Usually the custom controls are added via the asset gallery in Expression or the Toolbox in VS.

This is just for VS only now, I am not working on Expression Blend.
Yes you can think about it as an add-in to VS. I didn't decide yet on how the user interaction will be because I want to know what information I need to hold in order to add the controls automatically.
So, typically I will be doing an add-in and when the user clicks on a button I want to add some controls to the main window automatically at design time. Do you have an idea how this can be achieved?
I appreciate your time.

For those who were reading the posts, adding controls automatically in design time is done using ModelItems. You can get the main window model item as follows:
ModelService modelService = Context.Services.GetService();
ModelItem rootWindow = modelService.Root;

Where the Context is the EditingContext object, and usually you can have a reference to it through extensibility points like adorners, features, menu items....You can add ModelItems to the children of the main window ModelItem.
Any model item added will show in the design window and will be serialized to xaml code too, at design time.
In my case, I wanted to add some user controls from within a wizard which is started outside any of the extensibility options, and in that case I can't get the handle for an EditingContext object.

I am new to WPF. Can i use Windows user Control (Not WPF User control) in WPF application. If so, please give me a sample code.

Hi Atul,

Thanks for the links.
It works for me...


Hi everybody,
I am generating XAML page from xml file based on configuration and I placed diffrent control on it.

I want to add one xaml file into another xaml file in Tabcontrol different tabitem.

but I don't want to use code behind I want to do all in XAML code like

I have one xmal file

and one user control like below

and i another control


I want to add above both user control one after another in First XAML file how can I do ?

Do you have any idea. Please help me.

Ankit, I am not sure I got your problem correctly.

Please note that the statements you have shown are only for introducing the namespace and getting the prefix you want to work with. The user controls will be added in the XAML only once you add them as part of your container in the XAML and these are added just like another control like button, textbox etc that you may be using.

Thank you, thank you, thank you!

Adding the xmlns tag at the top is one of those things that when you forget it dives you nuts, I have been struggling with my custom control trying to figure out how to insert it for ages.

Stumble across your site and 30 seconds later it all works - You are a life saver :-)

Hi all

I need to integrate the Entity also into the usercontrol. But the connection is not open it give error like "Could not connect". I put the entity on the seperate layer

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Please key in the two words you see in the box to validate your identity as an authentic user and reduce spam.

Subscribe to this blog's feed

Follow us on

Blogger Profiles

Infosys on Twitter