I’m sure this is a real beginner question; I’m just having trouble figuring out how to search for it.
I have a simple UserControl (MyNewControl) that only has three controls, one of which is the following label:
<sdk:Label x:Name="Title" />
In another control, then, I want to use MyNewControl, like this:
<local:MyNewControl Grid.Column="1" x:Name="MyNewGuy" />
What do I need to do so that this second control can, for example, set a gradient background for my Title label?
First you define the desired dependency property in your UserControl:
To assign the value of your property to the child label, you can bind using the ElementName property of the binding:
As you are using Silverlight 5, you can also set a RelativeSource to your binding, instead of internally naming your UserControl:
Then, when using your UserControl, you just set (or bind) the LabelBackground to the desired value:
Just to note, you can also create a CustomControl instead of a UserControl, add the dependency property to it the same way and use a TemplateBinding when defining its template.