我正在設計類似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