Restrictions and Tips for Creating a Silverlight Control

When creating a custom control for Silverlight, follow the tips below for standards of use.

  • The control must derive from System.Windows.FrameworkElement.
  • The control must have a parameterless constructor.
  • Any property that you want to be able to configure through the Designer should be exposed as a standard Silverlight dependency property.
    • The property can be driven by a binding or expression in the Silverlight Designer.
    • For simple data types, the Designer provides a default property editor to all users so that a literal value for the property can be configured.
    • For data types where the default property editor is not sufficient, custom data type editors can be created. For more information, see Create a Custom Data Type Editor for the Silverlight Designer.
    • These properties should be of a data type that can be serialized to XAML.
      Note: There is a known issue with using GUID as a data type.
    • For complex data types, if a property using this type is not being persisted, it may help to initialize the default value of the property using the SetValue method as described below.
  • If you want the property to be configurable through the designer, the Editor-Browsable attribute should be set to Always or Advanced.
  • The Category attribute can be used to specify the category this property appears in.
Below is an example showing the instructions property to be exposed on My CustomControl. For the definition below, this property appears in the Designer with a default value of "Please enter in the values below." A different value for this property can be specified through the default property editor provided. In this case, it is a simple textbox. In addition, this property value can be driven by a binding or expression in the Silverlight Designer.

    public static readonly DependencyProperty InstructionsProperty 
= 
DependencyProperty.Register("Instructions", typeof(string), 
typeof(MyCustomControl), new 
PropertyMetadata("Please enter in the values below."));
    [EditorBrowsable(EditorBrowsableState.Always), 
Category("Definition")]
    public String Instructions
{
              get { return GetValue(InstructionsProperty) as 
String; }
              set { SetValue(InstructionsProperty, value); }
}    
         
For complex data types, a default value may need to be specified inside the control constructor instead of the DependencyProperty definition. It is required only if the custom property is not being serialized to XAML when the property is configured from within the Silverlight Designer. See the example below:
   public HelpfulDataGrid()
              { 
                  SetValue(PopupDataProperty, new CustomList());
                  //Rest of constructor code here
               }
After the control is created and tested from within Visual Studio, refer to Add a reference to a Silverlight assembly, which explains how to configure the control so it appears in the toolbox.