Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 729
adminSoftDK Avatar asked Nov 30 '15 15:11

adminSoftDK


People also ask

What is a UserControl?

The UserControl gives you the ability to create controls that can be used in multiple places within an application or organization.

What is the difference between custom and user control?

The main difference between them- User Control is a page file with extension . ascx which can only be used within a single application or project But custom controls are assemblies(dll files) that can be used in multiple applications.

What is user control object in Visual Basic?

In more detail, a user control is a VB.NET class. The class Inherits from the Framework UserControl class. The UserControl class gives your control the base functions it needs so it can be treated like the built-in controls. A user control also has a visual interface, much like a VB.NET form that you design in VB.NET.


1 Answers

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.

like image 76
Mike Eason Avatar answered Sep 28 '22 03:09

Mike Eason