WPF 数据网格性能 [英] WPF Datagrid Performance

查看:16
本文介绍了WPF 数据网格性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WPF Toolkit 数据网格,目前它的滚动速度非常慢.该网格有 84 列和 805 行.(包括 3 个固定列,标题是固定的.)水平和垂直滚动都非常慢.虚拟化已打开,我已在 xaml 中显式启用列虚拟化和行虚拟化.有什么可以真正影响性能的需要注意的地方,例如绑定方法,或者每个单元格模板中的 xaml 是什么?

I am working with the WPF Toolkit data grid and it is scrolling extremely slow at the moment. The grid has 84 columns and 805 rows. (Including 3 fixed columns and the header is fixed.) Scrolling both horizontally and vertically is extremely slow. Virtualization is turned on and I have enabled column virtualization and row virtualization explicitly in the xaml. Is there anything to watch out for that can really effect performance, such as binding methods, or what xaml is in each celltemplate?

需要注意的一件事是我在创建数据网格时动态添加列.那会不会有什么影响?(我还同时动态创建了 celltemplate,以便我的绑定设置正确.)

One thing to note is I am dynamically adding the columns on creation of the datagrid. Could that be effecting anything? (I also dynamically create the celltemplate at the same time so that my bindings are set right.)

以下是大多数生成的单元格的模板代码.基本上对于我需要动态添加的列(其中大部分是),我遍历我的列表并使用 AddColumn 方法添加列,另外我动态构建模板,以便绑定语句正确索引集合中的正确项目对于该列.模板并不太复杂,只有两个 TextBlock,但我确实在每个上绑定了四个不同的属性.通过更改 OneWay 的绑定,我似乎能够挤出更多性能:

Below is the code from the template for most of the cells that get generated. Basically for the columns I need to dynamically add (which is most of them), I loop through my list and add the columns using the AddColumn method, plus I dynamically build the template so that the binding statements properly index the right item in the collection for that column. The template isn't too complex, just two TextBlocks, but I do bind four different properties on each. It seems like I was able to squeeze out a little bit more performance by changes the bindings to 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);
    }

推荐答案

由于我看不到您的源代码,因此很难为您提供帮助.特别是因为 WPF 应用程序的性能受到很多因素的影响.有关注意事项的一些提示,请参阅优化 WPF 应用程序性能.是的 - 在每个单元格中使用什么 xaml 非常重要.因为通常性能问题确实归结为元素太多".您知道 TextBox 是 30 个单独的元素吗?我建议您使用 WPF 性能分析工具 以了解更多关于你的具体问题.尽量减少您使用的元素数量(例如,在适当的情况下从 TextBox 切换到 TextBlock).

Since I can't see your source code it is quite hard to help you. Especially since the performance of a WPF application is influenced by a lot of things. For some hints on what to look out for see Optimizing WPF Application Performance. And yes - it greatly matters what xaml is used in each cell. Because usually performance problems do boil down to "too many elements". Did you know that a TextBox are I think 30 individual elements? I recommend you use the Performance Profiling Tools for WPF to find out more about your specific problem. Try to minimize the amount of elements you are using (e.g. by switching from TextBox to TextBlock where appropriate).

此外,您还必须检查您尝试使用该应用程序的任何 PC 上是否存在性能问题.也许您使用的 PC 正在强制 WPF 进入基于软件的渲染.或者你在使用任何 BitmapEffects?

Also you have to check if the performance problems exist on any PC you try the application on. Maybe the PC you are using is forcing WPF into software based rendering. Or are you using any BitmapEffects?


查看您的代码,我建议您更改


Looking at your code I would suggest you change

column.Width = DataGridLength.Auto;

设置为合理的固定宽度,因为数据网格不必在每次发生变化(例如添加行,甚至滚动)时动态重新计算宽度.

to a reasonable fixed width, since the datagrid does not have to recalculate the width dynamically every time something changes (like adding rows, or even scrolling).

这篇关于WPF 数据网格性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆