我有一个itemcontrol,它有 3 个可能的数据模板。
<DataTemplate>
<StackPanel>
<TextBlock
FontSize="25"
FontWeight="Light"
Margin="0,8,0,5"
Text="{Binding Name}" >
</TextBlock>
<!-- Εδω τα items -->
<ContentControl
Content="{Binding Preferences}"
Name="items" >
</ContentControl>
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger
Binding="{Binding Path=SelectionMode}"
Value="1" >
<Setter
Property="ContentTemplate"
TargetName="items"
Value="{StaticResource SoloSelection}" />
</DataTrigger>
<DataTrigger
Binding="{Binding Path=SelectionMode}"
Value="2" >
<Setter
Property="ContentTemplate"
TargetName="items"
Value="{StaticResource MultiSelection}" />
</DataTrigger>
<DataTrigger
Binding="{Binding Path=SelectionMode}"
Value="3" >
<Setter
Property="ContentTemplate"
TargetName="items"
Value="{StaticResource MultiQuantitySelection}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
里面的东西是一个带有一些按钮的包装板。所以虚拟化似乎并不容易。我想要平滑滚动。问题是虽然按钮是一个边框和一个文本块,但它很慢。
我做了一个测试。
var sw = new Stopwatch();
sw.Start();
var vv = new SolidColorBrush(Colors.Red);
for (int i = 0; i < 150; i++)
{
// here is the operation that fills the control
var b = new Button();
b.Height = 65;
b.Width = 120;
b.Content = "Gamiese";
this.items.Items.Add(b);
}
this.Dispatcher.BeginInvoke(
DispatcherPriority.Loaded,
new Action(() =>
{
sw.Stop();
MessageBox.Show("Took " + sw.ElapsedMilliseconds + " ms");
}));
}
这在我的电脑上大约需要 40-50 毫秒,但在慢速电脑上需要 200 多毫秒。所以对于所有其他的东西,而不是这个简单的例子,它可以高达 600-900 毫秒。所以触摸体验可能是缓慢而痛苦的。我将此归咎于 WPF,因为我使用 QT QML 进行了类似的测试,并为更重的 200 个按钮创建了它,它立即点亮并且滚动流畅。因此,WPF 在此方面表现不佳。有什么我能做的吗?即使缓存按钮也无济于事。因为填充树似乎是个问题。渲染不是问题,因为在所有计算机上滚动都非常快。自定义绘图可能会解决我的问题,但我为什么要这样做?