开发者

ItemsControl in WPF generate all items even if Virtualization is enabled

I have a class that Inherits from ItemsControl and also a VirtualizedPanel that inherits from VirtualizedStack Panel, I created the templates so that the my Controls holds the Items开发者_运维技巧presenter inside a ScrollViewer and have all Vitualizing Properties enabled as well as the CanContentScroll.

The problem is that I'm using DataVirtualization in the back end so I don't have all the collection on memory and in WPF when the ItemsControl gets loaded it calls the GetEnumerator() so it tries to go all over the collection. In Silverlight this is not happening, the ItemsControl just call the visible items using the Indexer of my Collection which implements IList.

Is there a way to make the ItemsControl in WPF to just use the indexer instead of trying to load all the collection through the IEnumerable?


Virtualization is only applied to ListBox and ListView by default in WPF....try out using one of these controls...


I had the same problem when had been trying to implement a custom collection for my Control, inherited from ItemsControl. My collection implemented only IList and when I put it in ItemsSource only GetEnumerator method was called except the indexer. When I've added the inheritence from the IList it starts to call indexer.

Example of usage:

class MyClass : IList<T>, IList
{
  ...
        object IList.this[int index]
        {
            get { return this[index]; }
            set { throw new NotSupportedException(); }
        }

        public int this[int index]
        {
            get { return items[i]; }
            set { throw new NotSupportedException(); }
        }

        public IEnumerator<T> GetEnumerator()
        {
            for (int i = 0; i < count; i++)
            {
                yield return items[i];
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
  ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜