Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# treeview在运行几分钟后速度变得非常慢_C#_Wpf_Performance_Treeview - Fatal编程技术网

C# treeview在运行几分钟后速度变得非常慢

C# treeview在运行几分钟后速度变得非常慢,c#,wpf,performance,treeview,C#,Wpf,Performance,Treeview,正如标题中提到的,我在层次结构中有大量的项,我想在treeview控件中显示它们,除了加载时间外,在开始时性能还不错,但是使用treeview的时间越长(选中复选框、展开、收缩、选择),速度就越慢,下面是示例代码(元素数量的选择方式是,如果内存不足,它们代表正常使用的实际最大元素数量,只需调整创建项目的数量即可) XAML代码: <Window x:Class="testLargeContourTree.MainWindow" xmlns="http://schemas.m

正如标题中提到的,我在层次结构中有大量的项,我想在treeview控件中显示它们,除了加载时间外,在开始时性能还不错,但是使用treeview的时间越长(选中复选框、展开、收缩、选择),速度就越慢,下面是示例代码(元素数量的选择方式是,如果内存不足,它们代表正常使用的实际最大元素数量,只需调整创建项目的数量即可)

XAML代码:

<Window x:Class="testLargeContourTree.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <TreeView  Grid.Row="0" x:Name="treeView" ItemsSource="{Binding Children}">
                <!--<TreeView.Resources>
                    <Style TargetType="TreeViewItem">
                        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                    </Style>
                </TreeView.Resources>-->
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                        <StackPanel Orientation="Horizontal">
                            <CheckBox IsChecked="{Binding IsChecked}" />
                            <TextBlock Text="{Binding Name}" />
                        </StackPanel>                  
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>
            <Button Grid.Row="1" Content="Start" />
            <Button Grid.Row="2" Content="Stop" />
            <Button Grid.Row="3" Content="Move Up" />
            <Button Grid.Row="4" Content="Move Down" />
        </Grid>
        <TextBlock Grid.Column="1" x:Name="textOut" />
    </Grid>
</Window>

C#代码:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用系统线程;
使用System.Windows.Threading;
命名空间testLargeContourTree
{
/// 
///Interaktionslogik für MainWindow.xaml
/// 
公共部分类主窗口:窗口
{
私有MyTreeView项目集合;
公共主窗口()
{
初始化组件();
_itemCollection=新建MyTreeViewItem();
mytreevievetem年;
MyTreeViewItem月;
MyTreeViewItem日;
MyTreeViewItem作业;
MyTreeViewItem等高线;
mytreevietem段;
Int32 numberOfJobs=0;
Int32 numberOfSegments=0;
对于(Int32 i=0;i<2;i++)
{
年份=新的MyTreeViewItem(String.Format(“{0}”,2013+i));
对于(Int32 j=0;j<12;j++)
{
month=新的MyTreeViewItem(String.Format(“{0}”,(Months)j));
对于(Int32 k=0;k<10;k++)
{
day=新的MyTreeViewItem(String.Format(“{0}”,k+1));
对于(Int32 l=0;l<20;l++)
{
job=newMyTreeViewItem(String.Format(“job{0}”,numberOfJobs+1));
对于(Int32 m=0;m<50;m++)
{
contour=新的MyTreeViewItem(String.Format(“contour{0}”,m+1));
对于(Int32 n=0;n<100;n++)
{
段=新的MyTreeViewItem(String.Format(“段{0}”,n+1));
等高线。AddChild(段);
numberOfSegments++;
}
job.AddChild(等高线);
}
day.AddChild(工作);
numberOfJobs++;
}
月.日;
}
年.月;
}
_itemCollection.AddChild(年);
}
treeView.DataContext=\u itemCollection;
Text.Text=String.Format(“段数:{0}”,numberOfSegments+1);
}
}
公众普查月份
{
一月,
二月
前进
四月
也许
六月
七月
八月
九月
十月
十一月
十二月
}
公共类MyTreeViewItem:INotifyPropertyChanged
{
私有字符串\u名称;
公共字符串名
{
获取{return\u name;}
设置{u name=value;OnPropertyChanged(“name”);}
}
公共可观测集合子项{get;set;}
选择私有布尔函数;
公营部门选举产生
{
获取{return}isSelected;}
集合{u isSelected=value;OnPropertyChanged(“isSelected”);}
}
私有布尔函数_是展开的;
公共布尔扩展
{
获取{return\u isExpanded;}
设置{u isExpanded=value;OnPropertyChanged(“isExpanded”);}
}
检查私有布尔值;
检查公共布尔值
{
获取{return\u已检查;}
设置
{ 
_isChecked=值;
财产变更(“已检查”);
如果(子项!=null)
{
可观察收集儿童=儿童;
foreach(MyTreeViewItem child in children)
{
child.IsChecked=值;
}
}
}
}
公共MyTreeViewItem()
{
IsSelected=false;
IsExpanded=false;
IsChecked=false;
Name=“”;
Children=新的ObservableCollection();
}
公共MyTreeViewItem(字符串名称):this()
{
名称=名称;
}
公共void AddChild(MyTreeViewItem项)
{
添加(项目);
}
公共事件属性更改事件处理程序属性更改;
公共void OnPropertyChanged(字符串propertyName)
{
如果(房地产公司)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading;
using System.Windows.Threading;


namespace testLargeContourTree
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private MyTreeViewItem _itemCollection;

        public MainWindow()
        {
            InitializeComponent();

            _itemCollection = new MyTreeViewItem();
            MyTreeViewItem year;
            MyTreeViewItem month;
            MyTreeViewItem day;
            MyTreeViewItem job;
            MyTreeViewItem contour;
            MyTreeViewItem segment;
            Int32 numberOfJobs = 0;
            Int32 numberOfSegments = 0;
            for (Int32 i = 0; i < 2; i++)
            {
                year = new MyTreeViewItem(String.Format("{0}", 2013 + i));
                for (Int32 j = 0; j < 12; j++)
                {
                    month = new MyTreeViewItem(String.Format("{0}", (Months)j));
                    for (Int32 k = 0; k < 10; k++)
                    {
                        day = new MyTreeViewItem(String.Format("{0}", k + 1));
                        for (Int32 l = 0; l < 20; l++)
                        {
                            job = new MyTreeViewItem(String.Format("Job {0}", numberOfJobs + 1));
                            for (Int32 m = 0; m < 50; m++)
                            {
                                contour = new MyTreeViewItem(String.Format("Contour {0}", m + 1));
                                for (Int32 n = 0; n < 100; n++)
                                {
                                    segment = new MyTreeViewItem(String.Format("Segment {0}", n + 1));
                                    contour.AddChild(segment);
                                    numberOfSegments++;
                                }
                                job.AddChild(contour);
                            }
                            day.AddChild(job);
                            numberOfJobs++;
                        }
                        month.AddChild(day);
                    }
                    year.AddChild(month);
                }
                _itemCollection.AddChild(year);
            }
            treeView.DataContext = _itemCollection;
            textOut.Text = String.Format("number of segments: {0}", numberOfSegments + 1);
        }
    }

    public enum Months
    {
        Jannuary,
        February,
        March,
        April,
        May,
        June,
        July,
        August,
        September,
        October,
        November,
        December
    }


    public class MyTreeViewItem : INotifyPropertyChanged
    {
        private String _name;
        public String Name
        {
            get { return _name; }
            set { _name = value; OnPropertyChanged("Name"); }
        }
        public ObservableCollection<MyTreeViewItem> Children { get; set; }
        private Boolean _isSelected;
        public Boolean IsSelected
        {
            get { return _isSelected; }
            set { _isSelected = value; OnPropertyChanged("IsSelected"); }
        }
        private Boolean _isExpanded;
        public Boolean IsExpanded
        {
            get { return _isExpanded; }
            set { _isExpanded = value; OnPropertyChanged("IsExpanded"); }
        }
        private Boolean _isChecked;
        public Boolean IsChecked
        {
            get { return _isChecked; }
            set 
            { 
                _isChecked = value; 
                OnPropertyChanged("IsChecked");
                if (Children != null)
                {
                    ObservableCollection<MyTreeViewItem> children = Children;
                    foreach (MyTreeViewItem child in children)
                    {
                        child.IsChecked = value;
                    }
                }
            }
        }

        public MyTreeViewItem()
        {
            IsSelected = false;
            IsExpanded = false;
            IsChecked = false;
            Name = "";
            Children = new ObservableCollection<MyTreeViewItem>();
        }

        public MyTreeViewItem(String name) : this()
        {
            Name = name;
        }

        public void AddChild(MyTreeViewItem item)
        {
            Children.Add(item);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
<TreeView VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling">