TreeView UI虚拟化无法正常工作

时间:2014-08-05 10:38:15

标签: c# wpf treeview virtualization

我大约有大约127K的TreeViewItems,我需要让UI虚拟化工作。

我的XAML:

<TreeView ScrollViewer.CanContentScroll="True" ItemsSource="{Binding}" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling" Visibility="Hidden" Name="treeView" Margin="10,10,10,0" VerticalAlignment="Top" Grid.Column="0" SelectedItemChanged="treeView_SelectedItemChanged" BorderThickness="0"/>

和我的C#:

            treeView.Visibility = Visibility.Visible;
            articleIndexer = OpDb.Articles;
            int count = articleIndexer.GetCount();
            //if (count > 1000)
             //   count = 1000;
            IEnumerable<TBase3.Article> articles = articleIndexer.GetArticles(0, count);
            List<TreeViewItem> items = new List<TreeViewItem>();
            foreach (var article in articles)
            {
                TreeViewItem item1 = new TreeViewItem();
                item1.Header = article.Name;
                item1.Tag = new DbInfo() { RelatedId = article.BlockIndex, DbId = article.Database.Id, IsArticle = true };
                items.Add(item1);
                //treeView.Items.Add(items);
            }
            Items = items;
            treeView.DataContext = Items;

文章计数超过127K,并且在完成forearch循环后,它会查看我的TreeViewItems。但我的GUI是TILT,我需要结束进程来关闭应用程序。所以我猜测我的虚拟化不起作用,因为GUI没有响应。项目是IList

1 个答案:

答案 0 :(得分:0)

虚拟化仅适用于数据绑定。虽然您在此处进行绑定,但您仍在视图模型中创建TreeViewItems。那是错的。 TreeView应该决定它需要生成多少TreeViewItem。因此,不要在viewmodel中创建TreeViewItems,而是创建一个模型类来表示树节点。并绑定该模型类的集合。

        var list = new List<TreeModel>();

        for (int i = 0; i < 10000; i++)
        {
            var treeModel = new TreeModel();
            treeModel.Header = "Node 1";
            list.Add(treeModel);
        }

        MyTreeView.ItemsSource = list;