Wednesday, January 30, 2013

A Custom Control in Silverlight 5

In silverlight project I have seen using custom control are vast.every control should be custom according to requirement.I have known lot of things on that.But in searching in internet there also various site regarding on that . MSDN is one of them good resources and others site are also available describing the CustomControl good enough. Whatever I have seen Custom Controls are like Textboxes, Text Blocks, Combo boxes, DatePickes etc . Now it is a little bit confusing that when Microsoft is providing So many controls & u can easily get it in ur ToolBox of ur VisualStudio2010 Silverlight Projects. So what is the meaning of CustomControls & I have also written the example of customcontrols like TextBoxes, DatePicker….. Well , we are going little deep of its basics.

What is CustomControl?

Generally speaking a CustomControl is a loosly coupled control usually defined in a class, which derives from  an existing control and extends it functionality in different way. Though CustomControl  are defined within in a Class But , the visual effects (I mean the UI of custom Control) generally defined in Resource Dictionary inside the resource file . This makes it little complex. But, u have a freedom to make our desired control as we want.

What are the benefits of CustomControl?

CustomControls are reusable controls. Once u creates a custom Control u can simply use it in different projects by simply add the assembly in the projects. Yes , in our Visual Studio toolbox  what we do Simply Drag & drop our controls from this and use this as per requirement. But generally u can’t able to see ur newly make customcontrol in toolbox for that there need to do some additional steps . I’m skipping this at this moment . Not only CustomControl is a reusable controls but also they can be skinnable,themable in any projects.
One thing also is that we make our CustomControl because of sometimes ur Silverlight common control doesn’t cover our specific requirements to apply in Projects.
The buttons, Textboxes, Comboxes …. etc even usercontrol are also customcontrol. Usually a CustomControl is inherit from System.Windows.Controls.Control or  System.Windows.Controls.ItemsControl or System.Windows.Controls.ContentControl etc.
  • Controls : Represents the base class for UI elements that use a ControlTemplate to define their appearance.
  •  ItemsControl : Represents a control that can be used to present a collection of items.
  •  ContentControl : Represents a control with a single piece of content. Controls such as Button, CheckBox, and ScrollViewer directly or indirectly inherit from this class.

When to use CustomControl  ?

Now this is good idea when should I use CustomControl . I also write some description on it but  have  a look for clear idea.
  •  Use CustomControl   when you think that ur basic controls doesn’t provide much more functionality as u want , then pl make a customcontrol for urself.
  •  When  u want to apply theme to ur control ,use CustomControl  .
  • Suppose u have more than two projects in ur Silverlight Appliclications & using same components so it is better idea to make them a CustomControl  instead of  combinging or grouping them in UI and write code for multiple times .
  •  Use CustomControl   when u think to make a completely new control or a specific version of a control.
OK…I hope this gives a basic idea where & when to use CustomControl but may be there some specific requirements to u to make a Custom Control.
For understanding other flexibility of CustomControl u need to know about Usercontrol. And basic difference between CustomControl  & UserControls . Let’s see….

What is a user Control?

Use UserControl when u needs a fixed content control which will encapsulate related existing control and provides its own logics.  Also when you separate ur basic functionality of UI page(view page ) in to some smaller , manageable pieces of logics with reusability then use User Control. In Silverlight , generally we put the User Control as a root of element in application.
Now

Download




No comments:

Post a Comment