WPF DataGrid is very slow to render

后端 未结 10 2009
我在风中等你
我在风中等你 2020-12-23 02:30

I have tried using both a customized DataGrid as well as the stock one in WPF. I have tried populating them manually as well as through bindings. In both cases they are sl

相关标签:
10条回答
  • 2020-12-23 02:49

    A blog I found on Google gave me a sort-of solution. As the author said, I disabled GroupStyle, and the rendering speed issue was solved. But I needed grouping. The author said

    VirtualizingPanel.IsVirtualizingWhenGrouping
    

    is added to .NET 4.5. So I set it to true. Now rendering is quick with grouping. The problem is... scrolling is jerky. Not unacceptably jerky, but noticeably jerky. I had similar problem when I tried to create a TreeView with 2000+ nodes expanded. Without virtualisation, rendering was slow but scrolling was smooth. With virtualisation, rendering was quick but scrolling was jerky.

    Why can't we have both...

    0 讨论(0)
  • 2020-12-23 02:51

    For me it was:

    <Setter Property='ScrollViewer.CanContentScroll' Value='False' />
    

    I removed this from the style and the rendering became fast.

    0 讨论(0)
  • 2020-12-23 02:59

    A general tip for DataGrid performance issues: I had a problem with the DataGrid in which it took literally seconds to refresh after a window resize, column sort, etc. and locked up the window UI while it was doing so (1000 rows, 5 columns).

    It came down to an issue (bug?) with the WPF sizing calculations. I had it in a grid with the RowDefinition Height="Auto" which was causing the rendering system to try and recalculate the size of the DataGrid at runtime by measuring the size of each and every column and row, presumably by filling the whole grid (as I understand it). It is supposed to handle this intelligently somehow but in this case it was not.

    A quick check to see if this is a related problem is to set the Height and Width properties of the DataGrid to a fixed size for the duration of the test, and try running again. If your performance is restored, a permanent fix may be among these options:

    • Change the sizes of the containing elements to be relative (*) or fixed values
    • Set MaxHeight and MaxWidth of the DataGrid to a fixed value larger than it could get in normal use
    • Try another container type with different resizing strategy (Grid, DockPanel, etc)
    0 讨论(0)
  • 2020-12-23 02:59

    My problem was that I had ScrollViewer.CanContentScroll="False" set on my DataGrid. This disables virtualization all together for the DataGrid. More info about this can be found here:

    https://stackoverflow.com/a/3724695/4383302

    0 讨论(0)
提交回复
热议问题