Custom Controls
What is the need of Custom controls in c# ? and what are the advantages of custom controls over the user controls?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Prabhu RajaPosted Oct 18, 2011, 1:57 AM
Hemant KumarPosted Oct 18, 2011, 12:35 AM
UserControl : A
usercontrolis a reusable chunk of user interface that is built up as a composition of otherUIElementin the same style the main UI is built. In other words, a user control is just like a normal application block that can be used as Reusable component, and can be defined both usingXAMLand code together. It gives you a fresh UI canvas where you can define your custom reusable component that can be used widely in the application. In WPF, UserControl acts as a base class for any reusable component, but if you are looking for inheriting some other base, you can look into this.Limitation of UserControl :
1. Appearance of an UserControl cannot be changed using a
Template. Even though it has a property for Template, but it is of no use, as you cannot change the appearance of UserControl through this property.2. UserControl is derived from
ContentControl, thus if you change the Content of an usercontrol the entire UI will be replaced with that content.3. As UserControl has both XAML and code behind. But as XAML can be used only once for entire inheritance hierarchy, you cannot use XAML for any class that inherits from your userControl. This is actually because Application.LoadComponent loads up the XAML once and is incompatible with inheritance. Thus when loading the XAML, the IComponentConnector interface is used to hook events and fields from XAML, hence you cannot replace XAML from your base class.
Custom Control: A
customcontrolis a User interface element that has a distinct behaviour. ACustomControlis just a class that has one default appearance defined in Generic.xaml style which can be replaced by Template and Style at runtime but the behaviour you define for the control remains the same. So choose a CustomControl only when you need a certain kind of behaviour which is not there with the existing controls you have.Note: Please don't create a new custom control just to change the UI appearance as you can do this with any control available using custom Template
Limitation :
1. You have to define each behaviour for your control using Code. So it is hard way of achieving a behaviour.
2. Generic style is needed to be defined with your custom control to ensure that your control has a default look and feel.
Hence, based on your own requirement, if you are looking for a new behaviour which is different from existing userinterfaces available with WPF, you go for a Custom Control. A customControl can be styled and templated and best suited for a situation when you are building a Control Library.
On the contrary, a UserControl gives you an easy way to define reusable chunk of XAML which can be reused widely in your application and when you don't need to use it as a
Control Library.TulasiPosted Oct 18, 2011, 12:33 AM
If none of the existing ASP.NET server controls meet the specific requirements of your applications, you can create either a Web user control or a Web custom control that encapsulates the functionality you need.
Difference between custom control and user control
1)Custom controls compile to their own .dll file and can be used across applications.
User controls are limited to a single application.
2)Custom controls require no additional files beyond the .dll and are completely compiled.
User control must be deployed as an .ascx file, and the source code in the .ascx file will be visible to anyone with rights to view the files on the web server.
3)Custom controls can be display a design-time user interface that mimics what will be displayed at runtime.
User controls are always rendered as generic gray box.
4)Custom controls can be sized visually.
User controls can be sized only by setting properities.
5)Code to handle events exposed by custom controls can be largely generated automatically by VS.NET.
You will need to add all code by hand to handle an event exposed by a user control.
Tips
One should use a user control when reuse is only across a project.
When the control is used in a second project, take a few hours and turn it into a custom control.
have a look at this link http://msdn.microsoft.com/en-us/library/aa651710(VS.71).aspx
Please mark the answer as Accepted if it helps you.