2011-01-28 170 views
3

我正在设计类似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> 

+0

你好伙计。我有类似的问题。渲染速度很快,但创建缓慢。仍然没有找到解决方案 – GorillaApe 2013-06-14 21:33:36

回答

5

我怀疑是你触发许多布局更新,同时增加你的数百名儿童。

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

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

这将导致调度员停止处理消息时您添加控件,并做整个布​​局并在一个通渲染结束。

+1

看起来很有希望!当我有机会测试并告诉你结果时,将在未来24小时内回复。 – 2011-01-28 04:40:53

+1

刚刚尝试过。没有什么区别:( – 2011-01-28 14:40:35

1

如果您发现创建许多UserControl s操作太昂贵,那么您根本不需要创建UserControl s的解决方案又如何?

我决不是WPF的专家,但是如何使用数据绑定ListBoxListView来对照每个对象列表来表示被检查对象的一个​​属性?而不是一个StackPanel,你会有一个ListboxListView;而不是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's 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,并且还为每个项目类型使用不同的数据模板。