WPF - UserControls are very slow

I am creating something similar to a PropertyGrid where I want to show the properties of objects. For special reasons, I will not use the PropertyGrid, but create my own.

For each property, I created a custom user control. Now, to my horror, the performance is very poor. If I have something like 100 properties, it takes 500 milliseconds to display them in the StackPanel / Listbox.

I did an experiment in which I add 200 standard UserControls to the StackPanel. It took about 50 milliseconds. Still a very large number, I think.

Should usercontrol be used for this purpose? It seems very object oriented to do it this way, and I cannot see another solution.

However, I see that the PropertyGrid and TreeView work well, so what did they do and what should I do?

Edit:

        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        using (var suspend = Dispatcher.DisableProcessing())
        {
            // Add all children here
            for (int i = 0; i < 200; i++)
            {
                this.propertiesStackPanel.Children.Add(new System.Windows.Controls.Button(){Content = "Testing"});
            }
        }
        stopwatch.Stop();

It takes about 50 milliseconds anyway. If I switch to my own user control, it will be much higher. I could add that scrolling is not a problem.

Edit2:

OK This has nothing to do with the stackpanel. I found out that this is because creating UserControls is a very expensive operation. If you have a different idea of ​​what to do, I would love to hear them :)

Edit3: Nothing happens in the constructor of my usercontrol except the InitializeComponent method. Below is an example of user control that I am adding.

<UserControl
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"
mc:Ignorable="d"
x:Class="PropertyBox.GroupUC"
x:Name="UserControl"
d:DesignWidth="640" d:DesignHeight="480" Background="#FF32B595" BorderThickness="0">

<Grid x:Name="LayoutRoot">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="20px"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Border x:Name="border" BorderThickness="0,1" Grid.Column="1">
        <TextBox Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Right" BorderThickness="0" Padding="0" Visibility="Hidden"/>
    </Border>
    <Label x:Name="groupNameLabel" HorizontalAlignment="Left" Margin="5,0,0,0" VerticalAlignment="Center" Content="Label" Padding="0" Grid.Column="1"/>
    <Button x:Name="expandButton" HorizontalAlignment="Left" VerticalAlignment="Center" Width="12" Height="12" Content="" Click="ExpandButtonClick" Margin="4,0,0,0" Padding="0" Grid.ColumnSpan="2" d:IsHidden="True"/>
    <Image x:Name="expandButton2" Visibility="Hidden"  Width="12" Height="12" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="None"/>
</Grid>

+3
source share
2 answers

, , .

, :

using(var suspend = Dispatcher.DisableProcessing())
{
       // Add all children here
}

, , , .

+5

, UserControl , , UserControl?

WPF, ListBox ListView , ? StackPanel ListBox ListView; UserControl s, DataTemplate s.


:

, ; :

public namespace YourApplication
{
    public class Prop
    {
        public string Name { get; set; }
        public Type Type { get; set; }
    }

    public class Props : List<Prop> { }
}

XAML ( 100% , , ):

<Window ... xmlns:local="clr-namespace:YourApplication">
  <Window.Resources>
    <!-- this Prop list serves only as a demonstration in the XAML designer -->
    <local:Props x:Key="somePropsForDemonstration">
      <local:Prop Name="Name" Type="System.String" />
      <local:Prop Name="Age" Type="System.TimeSpan" />
    </local:Props>
  </Window.Resources>
  <!-- here your StackPanel replacement; bind it to a real items source -->
  <ListView ItemsSource={StaticResource somePropsForDemonstration}>
    <ListView.ItemsTemplate>
      <!-- this is a UI template that defines how a Prop object gets displayed;
           together with the Prop type, it replaces your UserControl -->
      <DataTemplate DataType="local:Prop">
        <StackPanel Orientation="Horizontal">
          <TextBlock Text="{Binding Name}" FontWeight="Bold" />
          <TextBlock Text=": " />
          <TextBlock Text="{Binding Type}" FontStyle="Italic" />
        </StackPanel>
      </DataTemplate>
    </ListView.ItemsTemplate>
  </ListView>
</Window>

, , "" ; . , Prop, .

+1

Source: https://habr.com/ru/post/1788390/


All Articles