Embed UserControl in Silverlight 2.0 Page


I am sure this makes perfect sense especially when you think about how ASP.Net used to work with usercontrols – but how to embed my usercontrol into my Silverlight page completely stumped me and I have to admit, actually took me some time to work out how to do. And as with all the other (often simple) things that stump me, I feel the urge to blog it so that next time I need to know how to do it, I know where to look. (That said, I hope I remember this from now on as it was actually quite simple!!)

So in ASP.Net if you wanted to add a reference to a user control, composite Control or server control, you would need to register a reference to it in the page header as shown here in MSDN and then you could work with the controls within your page.

Silverlight uses a similar concept where you need to add a reference to the assembly in the header, and then you can refer to it within your page XAML code UserControl Tag.

To check the correct values for the Namespace & Assembly values, you can check your Silverlight Tab in the properties of your application that contains the UserControl.

image

So for the example above, you replace the italicised names below with the names for your app:

<usercontrol ….

xmlns:UserControlName="clr-namespace:SampleApp;assembly=UserControlSample"

>

Then within your XAML code you can create the instance of the control using:

<UserControlName:usercontrol></UserControlName:usercontrol>

So simple now that I know how to do it… Don’t you just hate that?

Skip to main content