We pay for user submitted tutorials and articles that we publish. Anyone can send in a contributionLearn More
In my latest article I discussed Scrolling and Binding to large collections in WPF. We saw some disturbing behavior when binding a large collection to an ItemsControl. After Further Examination I found out very interesting things regarding that matter.
We saw that the application used about 500MB of Ram. Lets look at the code again:
What happens here is that because the ItemsControl has not Height Definition he grows to the size it needs in order to show ALL the items (50K in that application) making all of them drawn but not shown.The ScrollViewer gives you the option to scroll because the ItemsControl is much larger then him (he is restricted to the window size).
The correct way to implement this is the following:
The problem is that IT DOES NOT WORK!!! you will never get a ScroollBar this way, which brings me to my second point
After looking at the ItemsControl Template I realized why it just does not work, There is no ScrollViewer in the Template! This is the template:
Do you see a ScrollViewer? I don’t, therefore it will not work. God and Microsoft only knows why that property Exists, more on that matter in a different post, for now, lets solve this issue.
Once I changed the ItemsControl to a ListBox everything worked like a charm. This is the Code:
Here is the same image as in the first article:
Same amount of Objects, only 23MB of ram! We went from 500MB to 23MB and we just changed from ItemsControl to ListBox. WOW.
For the moment I think this is the best solution If you have a large amount of items, use a ListBox. And If you want to eliminate the selection just create a template for the selected items that makes him look as a regular one and that’s it.
Copyright © 2012 Dev102.com
Breeze : Designed by Amit Raz and Nitzan Kupererd