views:

158

answers:

3

I'm just getting my feet wet in Silverlight, and don't really understand the differences and pros/cons of creating a UserControl vs. creating a Control for the same task (as in when you right click on a selection in Expression Blend, for instance).

It seems like selecting "Make Into Control" just creates a new template for the base type you specify, whereas creating a UserControl creates a whole new base class. Is that correct?

Screenshot

In this particular instance, I'm creating a custom text box control that only takes numbers, and divides itself into 3 sections, storing 3 values into separate properties as pictured below. In this particular case, which would be best?

Screenshot 2

Update (Additional Question): Why can't I use Template Binding with a UserControl, but I can with a Control? That's one reason I thought that making a UserControl might not be the right decision.

+1  A: 

If you consider your control to be a group of three text boxes then a UserControl would be appropriate, but if your control will still essentially be a TextBox then you should extend the existing control with "Make into control."

It sounds like you need a UserControl to me.

GraemeF
+2  A: 

UserControls are meant to be a composite control - basically a bunch of other "controls" grouped together to work as a single, cohesive unit.

Custom Controls, on the other hand, are intended to be used as a single control. Think of the basic controls in the framework, such as TextBox or Button - if you were implementing something like that, you'd want a Control. (This is less common than UserControls, especially in WPF, since you can use templating on base class controls to accomplish quite a few things where you'd need custom controls in other frameworks). A custom Control is all about defining new behavior for a single "control."

Reed Copsey
Is there any way to use Template Binding with a UserControl?
Dov
Well, templates for individual elements in the composite control (ie: a TextBox will pick up a template assigned to TextBoxes) will apply to the usercontrol (provided it doesn't provide its own), but you don't get the same level of theming support as you do with a custom control.
Reed Copsey
+1  A: 

Dov, I think you've answered your own question with your update. Custom Controls are most useful when you want to make a control that supports templating. Otherwise they are useful when you are inheriting from other controls to cleanly add functionality (TextBox -> PasswordTextBox).

Adam Kinney
Yeah, a custom control definitely makes the most sense for me. Thanks.
Dov