2011-01-28 169 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,並且還爲每個項目類型使用不同的數據模板。