我正在设计类似PropertyGrid的东西,我想要显示对象的属性。由于特殊原因,我不打算使用PropertyGrid,但创建自己的。WPF - UserControls非常慢
对于每个属性我创建了一个自定义的usercontrol。现在我的恐怖表现非常糟糕。如果我拥有100个属性,则需要500毫秒才能将它们显示在StackPanel/Listbox中。
我做了一个实验,我添加了200个默认UserControl到一个StackPanel。它花了大约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();
这仍然需要大约50毫秒。如果我更改为我自己的自定义用户控件,它会更高。我可能会补充说,滚动不是问题。
EDIT2:
确定。它与stackpanel无关。我发现这是因为创建UserControls是一个非常昂贵的操作。如果你有什么做任何其他的想法,我会很高兴地听到他们:)
EDIT3:除InitializeComponent方法等我的用户的构造 没有是怎么回事。这是我添加的用户控件的一个例子。
<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>
你好伙计。我有类似的问题。渲染速度很快,但创建缓慢。仍然没有找到解决方案 – GorillaApe 2013-06-14 21:33:36