Adding A User Control to a Page in WPF

by Graeme
I decided to post this as a tip, as I came across it when attempting to do my first WPF application and couldn't get a quick and easy answer through Google.

Simply put there is no way to just drag a user control you have created onto another page in your WPF application. In order to use the control on the page you want to see it on, that page needs to add a reference to the namespace that the control is in. By default that namespace is the assembly of the application, thus you need to add something like the following to your window element in you page's xaml:

xmlns:my="clr-namespace:WPFFractalGenerator"

I've tagged the namespace my, so that when you add your control to the page it looks like <my:control1>. Below is an image of how it looks in the visual studio designer. Note that while the namespace says clr-namespace:<Your Namespace>, in the intellisense drop down you'll see <Your Namespace> in assembly XYZ.


(In my example WPFFractalGenerator is the application I'm building)




Then once you've added the namespace to your page you can begin adding UserControls or CustomControls you have created that belong to that namespace, like:



Hopefully, this help's someone out. As it doesn't seem to be an easy thing to find an example of on the web.

Posted at 7/9/2009 9:11:16 PM | Comments (0)
Submit a Comment:


© 2010 Graeme Finn