views:

52

answers:

1

Hi, I am developing controls for WP7 application. I am aware that I can quickly create control by inheriting from UserControl class (that derives from Control class). Also, that such controls cannot be customized, i.e., all the properties associated with control are those harcoded in user control - they cannot overridden in xaml etc. But can somebody please share the best practises / situations as to when to inherit directly from Control class vs ContentControl/ItemControl class vs UserControl class (am I missing any other options?)?

Thanks.

A: 

Here is a brief description of each control types that you mentioned:

1.ItemsControl - it is usually used when you want to have a control with items like for example ListBox,TreeView etc. One of the most important parts when deriving from ItemsControl is to override :

GetContainerForItemOverride

IsItemItsOwnContainerOverride

PrepareContainerForItemOverride

Here is an example:

http://www.silverlightshow.net/items/How-to-inherit-from-ItemsControl-and-create-a-UniformGrid-with-containers.aspx

Note that the sample is not with the latest Silverlight version but it explains in details how to implement a custom control.

2.Control - when you want to implement a simple control that has no Content or Items properties like a TextBox for example you can derive from Control.

3.ContentControl - when you need to place some content in the control.Like for example the button Content. It depends on your need what will be the choice of the base class.

You can also take a look at the rest of the tutorials that SilverlightShow provides connected with "how to implement a custom control".

I hope that this will answer your question.

FluentComponents