WPF Datagrid性能 [英] WPF Datagrid Performance

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

问题描述

我正在使用WPF Toolkit数据网格,现在滚动速度非常慢。网格有84列和805行。 (包括3个固定列,标题是固定的。)水平和垂直滚动都非常慢。虚拟化被打开,我已经在xaml中明确地启用了列虚拟化和行虚拟化。有没有什么值得注意的,可以真正影响性能,例如绑定方法,或每个celltemplate中的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方法添加列,加上我动态构建模板,使绑定语句正确索引集合中的正确项为那列。模板不是太复杂,只有两个TextBlocks,但我确实绑定了每个不同的属性。看来我通过更改绑定到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;

到合理的固定宽度,因为datagrid不必每次发生变化时动态地重新计算宽度(如添加行,甚至滚动)。

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 Datagrid性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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