Click here to Skip to main content
13,550,050 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 6 Oct 2010
Licenced CPOL

Embedding content inside a UserControl from its parent control in Silverlight 4

, 6 Oct 2010
Rate this:
Please Sign up or sign in to vote.
This trick explains how you can allow to embed Content for a UserControl when consumed by another control by making use of the ContentProperty
Whenever you create your own UserControl in Silverlight it bothered me that, when consuming the usercontrol in a parent control (e.g. the page), it is not allowed to access the user control's content.
e.g. assume the following code as part of a consumer control:

<Grid x:Name="LayoutRoot">
    <TextBlock Text="This is consumer defined content!"></TextBlock>

You get the following error: "The property 'Content' does not exist on the type....".

The problem is that the Content property is a private property of the UserControl.
However there is a workaround for this problem: the ContentProperty attribute.

Here are the steps to follow:
1. in your UserControl.xaml add a ContentPresenter control that will hold the user defined content. Name the ContentPresenter 'contentPresenter'.
2. in your UserControl codebehind define a new property:
public object UserContent
    get { return contentPresenter.Content; }
    set { contentPresenter.Content = value; }

3. Now for the trick: add the following attribute to the class:
[ContentProperty("UserContent")]<br />

There that's it. You can now create your layout witin the UserControl xaml and embed the actual user content at any position within your user control.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Herre Kuijpers
Architect Capgemini
Netherlands Netherlands
Currently Herre Kuijpers is employed at Capgemini Netherlands for over 10 years, where he developed skills with all kinds of technologies, methodologies and programming languages such as c#, ASP.Net, Silverlight, VC++, Javascript, SQL, UML, RUP, WCF. Currently he fulfills the role of software architect in various projects.

Herre Kuijpers is a very experienced software architect with deep knowledge of software design and development on the Microsoft .Net platform. He has a broad knowledge of Microsoft products and knows how these, in combination with custom software, can be optimally implemented in the often complex environment of the customer.

You may also be interested in...


Comments and Discussions

QuestionThanks Pin
Phylum12325-May-12 3:35
memberPhylum12325-May-12 3:35 
Thank you very much for this tidbit. Been looking for this solution for a while.

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180515.1 | Last Updated 7 Oct 2010
Article Copyright 2010 by Herre Kuijpers
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid