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 a 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"> <Setter.Value> <ControlTemplate TargetType="my:SampleControl"> <Grid><!--The control's core elemetns come here--></Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
Conclusion 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
No comments:
Post a Comment