adminSoftDK
adminSoftDK

Reputation: 2092

What is the purpose of an UserControl?

Why do we actually need a user control?

Window:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpfApplication1="clr-namespace:WpfApplication1">

    <wpfApplication1:SaveCloseUserControl />

</Window>

User control:

<UserControl x:Class="WpfApplication1.SaveCloseUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Orientation="Horizontal">
    <Button Height="30" Content="Save" />
    <Button Height="30"
            Margin="1"
            Content="Cancel" />
</StackPanel>
</UserControl>

Code behind:

public partial class SaveCloseUserControl : UserControl
{
    public SaveCloseUserControl()
    {
        InitializeComponent();
    }
}

I don’t see any reason why should I wrap a StackPanel (or any other control) inside of a UserControl, if the following code without UserControl will do exactly the same.

Window:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpfApplication1="clr-namespace:WpfApplication1">

    <wpfApplication1:SaveCloseStackPanel />

</Window>

Stack panel without user control:

<StackPanel x:Class="WpfApplication1.SaveCloseUserControl"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Orientation="Horizontal">
    <Button Height="30" Content="Save" />
    <Button Height="30"
            Margin="1"
            Content="Cancel" />
</StackPanel>

Code behind:

 public partial class SaveCloseUserControl : StackPanel
 {
    public SaveCloseUserControl()
    {
        InitializeComponent();
    }
 }

I’ve been using UserControls everywhere, but now when I think about it, they don’t do anything apart from wrapping an item in it. So I tried it on 10 different views, and does not matter what it is, I was able to replace the UserControl with other items (Grid, ComboBox, GroupBox etc), and it all works exactly the same way. So to be clear, if I had a user control and first thing in it was ComboBox, then I removed UserControl and put ComboBox in its place. Everything inside then stayed as it was, just like the above example with StackPanel.

Why would I even bother with UserControl, and have another item to be created and rendered if it does not do anything?

Upvotes: 16

Views: 7050

Answers (4)

Dhruv Panchal
Dhruv Panchal

Reputation: 60

As per your example, If you are using user control for one time only then it is not so useful. But In such scenarios, If some design part is used so many times and every time you write a xaml code or any other logic to create the same UI, then User control comes to the play.

Upvotes: 1

tgpdyk
tgpdyk

Reputation: 1233

I have read somewhere from a book (sorry forgot the book title...) that UserControl is a "block box" container.

As my understanding to the term and using your example as the basis, you can see that the StackPanel derived one is constrained to the ability of StackPanel to layout the two buttons - which is either Horizontal or Vertical. In the future, if this layout changes (maybe a new requirement?) you will need to create a new panel derived class (Or rewrite the existing one) and use it, say a DockPanel. This is bad.

While on the UserControl derived one, all you have to do is change the StackPanel to another panel type that suit the new requirement.

That makes the UserControl a block box, IMHO.

Upvotes: 2

Simon Mourier
Simon Mourier

Reputation: 138906

The purpose of UserControl is to help you build reusable UI components that can be added to your toolbox just like a built-in control, starting from nothing. The prefix "User" here kinda means "Not from the WPF team". WPF does not ship any class that derives from UserControl.

A very important aspect of UserControls is you can design them using XAML (so they can be composite), and pack code + XAML together, possibly in a "library" assembly that you can ship without the source.

Now, your examples are quite anemic (no offence :-), they don't do anything so interesting that I would be tempted to make them really reusable. Most of the time, you will build UserControls from usage (or experience), after having realized that you have repeated the same XAML pattern more than once, maybe with a few variation.

For example in this open source project (Github for Visual Studio): https://github.com/github/VisualStudio/tree/master/src/GitHub.UI/Controls, you'll see they have written some custom controls, like EmojiImage (that derives from Image, and has no need for XAML), and one UserControl: HorizontalShadowDivider. Why? Because HorizontalShadowDivider has XAML associated with it and is (probably) used in more than one place.

Upvotes: 3

Mike Eason
Mike Eason

Reputation: 9713

The purpose of a UserControl is to group a set of controls into one, reusable component. They cannot be styled or templated.

The purpose of a Custom Control is to extend an existing control, or to create a brand new control. These, as opposed to a UserControl can be styled and templated.

I think you're getting mixed up with the two.

So, you may be wondering, "When should I use a UserControl and when should I use a Custom Control?" and the answer to that is it depends.

You should use a UserControl when you need to create a logical group of controls which interact in some way to create an almost composite control. You should use a Custom Control when you want to add functionality to an existing control.

In your example, your best approach would be to use a UserControl, as your StackPanel is a group of controls made into one reusable component.

You can find out a bit more here, and here.

Upvotes: 19

Related Questions