Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance UserControl / CustomControl

I already searched a lot of sites on the net, but didn't find any solution. The statement is, that there is no performance difference between a UserControl and a CustomControl.

But I have the following test class X, UserControl, CustomControl and MainWindow:

public class X : INotifyPropertyChanged
{
    private string _title;
    public string Title

    {
        get
        {
            return _title;
        }
        set
        {
            if (value == _title)
            {
                return;
            }
            _title = value;
            OnPropertyChanged("Title");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

UserControl:

<UserControl x:Class="controlperformance.DisplayView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"> 

<Grid Name="root" Background="LightGray">
    <TextBlock Text="{Binding Title}" />
</Grid>

</UserControl>

CustomControl:

public class DisplayControl : Control
{
    #region Title

    public string Title
    {
        get
        {
            return (string)GetValue(TitleProperty);
        }
        set
        {
            SetValue(TitleProperty, value);
        }
    }

    public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title",
                                                                                          typeof(string),
                                                                                          typeof(DisplayControl),
                                                                                          new PropertyMetadata(default(string)));

    #endregion

    static DisplayControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(DisplayControl), new FrameworkPropertyMetadata(typeof(DisplayControl)));
    }
}

Xaml:

 <Setter Property="Template">
       <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:DisplayControl}">

                <Grid Background="white">
                    <TextBlock Text="{TemplateBinding Title}" />
                </Grid>

            </ControlTemplate>
        </Setter.Value>
  </Setter>
</Style>

MainWindow:

public partial class MainWindow : Window
{
    Stopwatch sw = new Stopwatch();

    public MainWindow()
    {

        InitializeComponent();

        Loaded += OnLoaded;

        sw.Start();

        ObservableCollection<X> list = new ObservableCollection<X>();
        Random r = new Random();

        for (int i = 0; i < 50000; i++)
        {
            list.Add(new X { Title = r.Next().ToString()});
        }

        itemscontrol.ItemsSource = list;
    }

    private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        sw.Stop();
        MessageBox.Show(sw.Elapsed.ToString());
    }
}

MainWindow Content:

<ItemsControl Name="itemscontrol">
            <ItemsControl.ItemTemplate>
                <!--<DataTemplate DataType="{x:Type Controlperformance:X}">
                    <Controlperformance:DisplayView DataContext="{Binding}" />
                </DataTemplate>-->
                <DataTemplate DataType="{x:Type Controlperformance:X}">
                    <Controlperformance:DisplayControl Title="{Binding Title}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
</ItemsControl>

When using the CustomControl, the MessageBox shows approx. 20 Seconds on my computer, but when using the UserControl, it takes about a minute! Replacing the Control with its Grid an TextBox, it is even faster than the CustomControl (~16 sec).

Can someone see where the bottleneck is? The problem raises in my real-world application, where the Template/Control would be much more complex.

Thanks a lot,

micro

like image 681
micro Avatar asked May 30 '13 23:05

micro


People also ask

What is UserControl?

User controls are containers into which you can put markup and Web server controls. You can then treat the user control as a unit and define properties and methods for it. Custom controls. A custom control is a class that you write that derives from Control or WebControl.

What is the difference between user control and custom 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 the difference between user control and custom control in WPF?

A customControl can be styled and templated and best suited for a situation when you are building a Control Library. On the contrary, a UserControl gives you an easy way to define reusable chunk of XAML which can be reused widely in your application and when you don't need to use it as a Control Library .

What is a UserControl c#?

Definition of C# User Control. C# user control is defined as an implementation in programming language of C# to provide an empty control and this control can be leveraged to create other controls. This implementation provides additional flexibility to re-use controls in a large-scale web project.


1 Answers

This is a late answer but the basic difference is that a User Control is almost like a window in that you have the the control itself and then other controls like, buttons, grids, textboxes, etc can be added to it. The basic difference between a window and User Control is that the User Control can and must be displayed within a window.

A Custom Control on the other hand is just a control, it can be used to create a control with a specific functionality for which there are no built in controls or to give an existing control, like buttons, textboxes, etc a specific style to match the theme of your application. You can also add in extra features to existing controls by using a custom control such as adding a label to a text box to show it's purpose.

The difference in loading time is essentially a reflection of the different purposes of User and Custom Controls, with a User Control it load the control and the elements within that control so the loading time may be longer. With a Custom Control only the control itself must load so the so it won't take any longer to load than most built in WPF controls i.e. a button Custom Control shouldn't take longer than a built in button control.

like image 150
CJK Avatar answered Sep 18 '22 14:09

CJK