views:

95

answers:

2

Hi,

At our work we have a controversy going on where some people want to create user controls that combine frequently used controls like a label and a text box, or a label and a image control. That is, something like this:

<StackPanel Orientation="Horizontal">
    <Image Source="/Someimage/Somewhere.gif"/>
    <Label>Some text, hyperlink, or other content</Label>
</StackPanel>

and use it like this

<ImageLabel
    HeaderImageSource="/Someimage/Somewhere.gif"
>
    Some text, hyperlink, or other content
</ImageLabel>

The question is if they are providing enough encapsulation and abstraction to merit a separate user control?

+2  A: 

Yes, in my opinion this is sufficient to create a separate user control - if they form a logical component in your context. In the project I'm working on we do create user controls if we have grouped components like this we want to reuse.

The main benefit is that you get control of the structure and style of the component. Consider the case where you want to do a change somewhere. E.g. add a border around the image or add some style to your label. You don't want to browse the code to find all occurences of your repeated group of components. Instead - you'd like to update this one place only - namely in the custom component you've separated out.

Another advantage is if you want to bind the components to the same object. Then you can bind your ImageLabel to the object instead, and your components can bind directly to properties within this object in a nice and clean way.

Note: I'm assuming that you actually want to reuse this component and want it to be structured and styled the same way all through your application. I would never create a user control this simple for using only once.

stiank81
shouldnt styles do that ?
Yes, you can use styles for making sure the individual components look the same - but then you need to remember to update the style reference for each component. If you actually want them to look the same wherever they are used - as one logical component you should make it a user control. If you e.g. change style on your label, but forget to update it once you will get one occurence of this "component" which looks different from the others. I find it more clean to build separate components. But again; if they are actually logically bound together somehow.
stiank81
the control is proposed as a generic solution to wherever an image has to be used with a label
Then I'd go for a control..
stiank81
+2  A: 

Seems a little bit overkill to create a UserControl for three controls; I'd consider whether or not it saved me time and effort rather than encapsulation/abstraction.

Will