Silverlight provides you to create both a user control and a template control. Many people face the question “What should I use, a user control or a template control?”. In this post, I will attempt to solve your problem.

User Control
A user control is the simplest and should be used when you are creating a control specific to your application or scenario. A user control is not that flexible and does not allow the application author to stylize the control. An application can interact with or change the user control only if the user control author adds explicitly adds functionality by defining methods or properties that the application author can call or set. The application author should not try to access or modify the elements and controls placed inside a user control.(So that this does not happen the user control author can set the x:FieldModifier property to private, the default is internal.)

Template Control
A template control is one level more complex to implement than a user control. A template control is highly flexible and anyone can override its default styling. Generic controls that provide some special functionality should be template controls. All the controls inside the toolkit library are hence template controls. Another example of a template control would be a control similar to a password box but can switch to a text box using a property. This control can than by stylized to even contain a check box for doing the switch without changing any line of code. A Template Control is defined using a control template, which can be customized at will by the application author. The template control author must however take care that an application author may omit a certain part of the template, which may result in incorrect behavior or worse throw an exception.  To know how to create a new template control by using a control template provides all the information. This link is a must read to understand he basics of a template control, which I cannot elaborate here. Although, here is a sample of a template control, just for reference

<Style TargetType="my:SampleControl">
    <Setter Property="Template">
            <ControlTemplate TargetType="my:SampleControl">
                <!--The control's core elements come here-->

Use a template control if you desire your control to be customizable, if modularity is the only purpose of the control then stick to the user control.

Related Links


One thought on “Choosing between user control and template control

Share your thoughts

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s