Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When Should You Extract a UserControl in WPF

Tags:

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?

like image 688
np-hard Avatar asked Dec 22 '09 13:12

np-hard


1 Answers

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.

like image 90
stiank81 Avatar answered Oct 11 '22 16:10

stiank81