我正在使用WPF Toolkit数据网格,此刻滚动速度非常慢。网格有84列和805行。 (包括3个固定列和标题是固定的。)水平和垂直滚动非常慢。虚拟化已打开,我已在xaml中明确启用了列虚拟化和行虚拟化。有什么需要注意的,这可以真正影响性能,例如绑定方法,或每个celltemplate中的xaml是什么?
需要注意的一点是,我在创建datagrid时动态添加列。这会影响到什么吗? (我也同时动态创建celltemplate,以便我的绑定设置正确。)
以下是生成的大多数单元格的模板代码。基本上我需要动态添加的列(大多数是这些),我遍历我的列表并使用AddColumn方法添加列,另外我动态构建模板,以便绑定语句正确索引集合中的正确项目该专栏。模板不是太复杂,只有两个TextBlocks,但我确实在每个上绑定了四个不同的属性。通过更改绑定到OneWay,我似乎能够挤出更多的性能:
private void AddColumn(string s, int index)
{
DataGridTemplateColumn column = new DataGridTemplateColumn();
column.Header = s;
//Set template for inner cell's two rectangles
column.CellTemplate = CreateFactViewModelTemplate(index);
//Set Style for header, ie rotate 90 degrees
column.HeaderStyle = (Style)dgMatrix.Resources["HeaderRotateStyle"];
column.Width = DataGridLength.Auto;
dgMatrix.Columns.Add(column);
}
//this method builds the template for each column in order to properly bind the rectangles to their color
private static DataTemplate CreateFactViewModelTemplate(int index)
{
string xamlTemplateFormat =
@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column=""0"" MinHeight=""10"" MinWidth=""10"" HorizontalAlignment=""Stretch"" Padding=""3 1 3 1"" TextAlignment=""Center"" Foreground=""{Binding Path=FactViewModels[~Index~].LeftForeColor,Mode=OneWay}"" Background=""{Binding Path=FactViewModels[~Index~].LeftColor,Mode=OneWay}"" Text=""{Binding Path=FactViewModels[~Index~].LeftScore,Mode=OneWay}"" />
<TextBlock Grid.Column=""1"" MinHeight=""10"" MinWidth=""10"" HorizontalAlignment=""Stretch"" Padding=""3 1 3 1"" TextAlignment=""Center"" Foreground=""{Binding Path=FactViewModels[~Index~].RightForeColor,Mode=OneWay}"" Background=""{Binding Path=FactViewModels[~Index~].RightColor,Mode=OneWay}"" Text=""{Binding Path=FactViewModels[~Index~].RightScore,Mode=OneWay}"" />
</Grid>
</DataTemplate>";
string xamlTemplate = xamlTemplateFormat.Replace("~Index~", index.ToString());
return (DataTemplate)XamlReader.Parse(xamlTemplate);
}
答案 0 :(得分:20)
由于我看不到您的源代码,因此很难为您提供帮助。特别是因为WPF应用程序的性能受到很多因素的影响。有关要注意的内容的一些提示,请参阅Optimizing WPF Application Performance。是的 - 在每个单元格中使用xaml非常重要。因为通常性能问题归结为“太多元素”。您是否知道TextBox是我认为的30个独立元素?我建议您使用Performance Profiling Tools for WPF了解有关特定问题的更多信息。尽量减少您正在使用的元素数量(例如,在适当的情况下从TextBox切换到TextBlock)。
此外,您还必须检查在尝试应用程序的任何PC上是否存在性能问题。也许你正在使用的PC正在迫使WPF进入基于软件的渲染。或者你在使用任何BitmapEffects?
编辑:
看看你的代码,我建议你改变
column.Width = DataGridLength.Auto;
到一个合理的固定宽度,因为数据网格不必在每次发生变化时动态地重新计算宽度(比如添加行,甚至滚动)。
答案 1 :(得分:10)
DataGrid性能问题的一般提示:我在使用DataGrid时遇到问题,在窗口调整大小,列排序等等之后刷新了几秒钟,并锁定了窗口UI(1000行) ,5列)。
使用WPF大小计算得出问题(bug?)。我在RowDefinition Height =“Auto”的网格中得到它,这导致渲染系统尝试通过测量每个列和行的大小来尝试重新计算DataGrid的大小,可能是通过填充整个网格(据我所知)。它应该以某种方式智能地处理它,但在这种情况下它不是。
快速检查以确定这是否是一个相关问题是在测试期间将DataGrid的高度和宽度属性设置为固定大小,然后再次尝试运行。如果您的性能已恢复,则可以在以下选项中进行永久性修复:
答案 2 :(得分:2)
在我的一个项目中,以下网格样式设置导致了一个主要的性能问题:
<Style TargetType='{x:Type controls:DataGrid}'>
<Setter Property='ScrollViewer.CanContentScroll' Value='False' />
...
当我删除ScrollViewer.CanContentScroll设置时,性能问题就消失了。
答案 3 :(得分:1)
您是否有机会安装任何类型的平板电脑(通过USB或平板电脑)?
我在使用平板电脑时发现了WPF数据网格中的性能错误。我发布了一段视频,MS here in this thread
确认了该视频干杯, 乔恩
答案 4 :(得分:1)
我有一个案例,我的基础对象只有setter属性。通过在集合中实现ITypedList并通过单个对象上的TypeDescriptionProvider / ICustomTypeDescriptor可以访问相同的属性。删除属性或添加getter解决了性能问题。
答案 5 :(得分:1)
我在这种情况下建议的一件事是看看你如何应用样式以及每个单元格的样式。 如果它具有复杂的可视树,则应用样式确实会降低性能。
您还可以在最新的WPF Datagrid上尝试Deferred Scrolling选项。