Search code examples
wpfxamluser-controls

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?


Solution

  • 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.