I've created a custom control for use in my Silverlight application. Its template is defined in the control library's Generic.xaml. Is there any way to set properties of the items in that template from the control's .cs file?
+3
A:
If you call GetTemplateChild(string childName)
with the name of your element as defined in the XAML, for example:
<Border x:Name="MyBorder" Background="Blue" ... />
then you can change the properties of the item. You must obviously cast the returned DependencyObject
to the correct type and check it's not null - just in case:
Border myBorder = GetTemplateChild("MyBorder") as Border;
if (myBorder != null)
{
myBorder.Backround = new SolidColorBrush(...);
}
You need to call this after OnApplyTemplate
has been called.
ChrisF
2009-12-17 21:49:51
Ah, that explains why there's no code completion on it. Thanks.
Dov
2009-12-17 21:50:52