C# WPF-UserControls非常慢

C# WPF-UserControls非常慢,c#,wpf,user-interface,user-controls,propertygrid,C#,Wpf,User Interface,User Controls,Propertygrid,我正在设计类似于PropertyGrid的东西,在这里我想显示对象的属性。由于特殊原因,我不打算使用PropertyGrid,而是创建自己的 对于每个属性,我都创建了一个自定义usercontrol。现在让我感到恐怖的是,演出非常糟糕。如果我有大约100个属性,那么在StackPanel/Listbox中显示它们需要500毫秒 我做了一个实验,在StackPanel中添加了200个默认用户控件。大约花了50毫秒。我认为这仍然是一个很高的数字 我是否应该使用usercontrols来实现此目的?这

我正在设计类似于PropertyGrid的东西,在这里我想显示对象的属性。由于特殊原因,我不打算使用PropertyGrid,而是创建自己的

对于每个属性,我都创建了一个自定义usercontrol。现在让我感到恐怖的是,演出非常糟糕。如果我有大约100个属性,那么在StackPanel/Listbox中显示它们需要500毫秒

我做了一个实验,在StackPanel中添加了200个默认用户控件。大约花了50毫秒。我认为这仍然是一个很高的数字

我是否应该使用usercontrols来实现此目的?这样做似乎非常面向对象,我真的看不到其他解决方案

然而,我可以看到PropertyGrid和TreeView表现良好,那么他们做了什么,我应该做什么

编辑:

        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();
秒表秒表=新秒表();
秒表。开始();
使用(var suspend=Dispatcher.DisableProcessing())
{
//在此处添加所有子项
对于(int i=0;i<200;i++)
{
this.propertiesStackPanel.Children.Add(new System.Windows.Controls.Button(){Content=“Testing”});
}
}
秒表;
这仍然需要大约50毫秒。如果我改为我自己的自定义用户控件,它会高得多。我可以补充一点,滚动不是问题

Edit2:

        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();
嗯。它与stackpanel无关。我发现这是因为创建用户控件是一项非常昂贵的操作。如果你还有其他想法,我很乐意听他们说:)

Edit3: 除了InitializeComponent方法之外,我的usercontrol的构造函数中没有发生任何事情。下面是我正在添加的usercontrol的一个示例

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


我怀疑您在添加数百个孩子的同时触发了许多布局更新

如果这是瓶颈,你可能要考虑做:

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

这将导致调度器在您添加控件时停止处理消息,并在最后一次性完成整个布局和渲染。

看起来很有希望!将在接下来的24小时内回复,当我有机会测试它并告诉您结果时。@BOBhink:有趣-您的UserControl在其构造函数中工作吗?你能发布代码显示你在构建/添加过程中正在做什么吗?你好,伙计。我也有类似的问题。渲染速度很快,但创建速度非常慢。仍然没有找到解决办法