如何懒惰评估wpf:DataGrid,仅根据需要检索数据 [英] How to lazy-evaluate a wpf:DataGrid, retrieving data only as needed

查看:295
本文介绍了如何懒惰评估wpf:DataGrid,仅根据需要检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF数据网格,绑定到从数据库linq-to-sql填充的列表。绑定是双向的,允许用户更改每一行的值

 < wpf:DataGrid AutoGenerateColumns =False 
ItemsSource ={Binding MyList}
SelectedItem ={Binding SelectedItem}>

当显示大约20000行时,程序在初始化时出现内存不足异常列表。即使行数较少,性能变得无法忍受。



我知道在初始化时,datagrid遍历每一行以测量最大列宽和其他属性。我们尝试将数据网格绑定到 myQuery.ToList()



/ code>(允许通过点击列排序数据网格)或直接绑定到IQueryable。 (排序不起作用)



两者都产生相同的结果。单独使用20000个项目的ToList()不会导致大量内存消耗,只有当它绑定到datagrid时才会发生。



忽略20000行有用的问题在一个datagrid中(这些是当前的要求;要改变一个工作示例将会有所帮助)。



最简单的方式是懒惰地加载当前的数据显示在屏幕上,忽略所有其他内容,直到滚动到视图中?



这可以在没有第三方库和主要代码更改的情况下完成吗?



如果没有,建议的解决方法是什么?

解决方案

事实证明,问题完全是用户错误我的部分:



WPF Datagrid可以做UI虚拟化只是罚款:消耗行的对象只需要时绘制;如果一行在数据网格的可见边界之外,则不会被实例化。



如果数据网格包含在ScrollViewer中,这将不起作用。
在一个滚动浏览器的内部,datagrid的每个部分都是可见的,所以整个datagrid将被渲染。滚动浏览器然后仅显示适合UI窗口的此渲染数据网格的一部分。



由于scrollviewer内的datagrid看起来像一个管理自己的滚动条的datagrid,我没有注意到scrollviewer。



删除滚动浏览器时,即使具有可变的高度和宽度,大量行也完全没有问题。 datagrid只是填充可用空间,只需要根据需要实例化新行。



所以简而言之,我的问题的解决方案是:不要放一个datagrid滚动浏览器内


I have a WPF datagrid, bound to a list populated by linq-to-sql from a database. The binding is two-Way, allowing the user to change the values in each row

        <wpf:DataGrid AutoGenerateColumns="False" 
              ItemsSource="{Binding MyList}" 
              SelectedItem="{Binding SelectedItem}" >

When displaying about 20000 rows, the program crashes with an out-of-memory exception during initialisation of the list. Performance becomes unbearably slow even with less rows.

I know that on initialization the datagrid iterates through each row to measure the maximum column width and other properties. It will apparently do so for all rows regardeless of whether they are on screen.

I tried either binding the datagrid to myQuery.ToList() (to allow sorting the datagrid by clicking on the columns) or binding directly to the IQueryable. (Sorting does not work with that)

Both produce the same result. The ToList() with 20000 items alone does not cause the massive memory consumption, this happens only when it is bound to the datagrid .

Ignoring the issue of how useful 20000 rows in a datagrid are (those are the current requirements; to change those a working example would be helpful).

What is the most simple way to lazily load only the data currently shown on the screen, and ignore everything else until it is scrolled into view?

Can this be done without third party libraries and major code changes?

If not, what would be the recommended workaround?

解决方案

It turns out that the problem was entirely user error on my part:

WPF Datagrid can do UI virtualisation just fine: The memory consuming row objects are only drawn when required; if a row is outside the visible bounds of the datagrid, it will not be instantiated.

This will however not work if the datagrid is contained inside a ScrollViewer. Inside a scrollviewer every part of the datagrid is virtually visible, so the entire datagrid will be rendered. The scrollviewer then shows only the part of this rendered datagrid that fits inside the UI window.

Since a datagrid inside a scrollviewer looks just like a datagrid that manages its own scrollbars, I did not notice the scrollviewer.

With the scrollviewer removed, large amounts of rows cause no problem at all, even with variable height and width. The datagrid simply fills the available space, and only instantiates new rows as required.

So in short, the solution for my problem was: Do not put a datagrid inside a scrollviewer

这篇关于如何懒惰评估wpf:DataGrid,仅根据需要检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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