Performance UserControl / CustomControl
Asked Answered
F

1

9

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

Fie answered 30/5, 2013 at 23:59 Comment(0)
S
1

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.

Sadonia answered 5/5, 2015 at 3:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.