Ejemplo n.º 1
0
        //当Item展开时预先对其子项目进行填充,注意是子项目,之所以这样做是因为,子项目中如果含有其他项目,将会在图标上显示三角形符号,表示含有子目录
        protected override void OnExpanded(RoutedEventArgs e)
        {
            base.OnExpanded(e);

            foreach (object obj in this.Items)
            {
                DirectoryTreeViewItem item = obj as DirectoryTreeViewItem;
                item.Populate();
            }
        }
Ejemplo n.º 2
0
        public void RefreshTree()
        {
            this.BeginInit();       //执行后,TreeView控件会暂停进行显示的更新,不这么做会导致树的创建速度变慢
            this.Items.Clear();

            DriveInfo[] drives = DriveInfo.GetDrives();

            foreach (DriveInfo drive in drives)
            {
                char chDrive = drive.Name.ToUpper()[0];
                DirectoryTreeViewItem item = new DirectoryTreeViewItem(drive.RootDirectory);

                //如果是只读的就显示卷标,否则就显示驱动器类型
                if (chDrive != 'A' && chDrive != 'B' && drive.IsReady && drive.VolumeLabel.Length > 0)
                {
                    item.Text = string.Format("{0} ({1})", drive.VolumeLabel, drive.Name);
                }
                else
                {
                    item.Text = string.Format("{0} ({1})", drive.DriveType, drive.Name);
                }

                if (chDrive == 'A' || chDrive == 'B')       //软盘驱动器
                {
                    //item.SelectedImage = item.UnselectedImage = new BitmapImage(new Uri("pack://application:,,/Images/floppy_disk.png"));
                    item.SelectedImage = item.UnselectedImage = new BitmapImage(new Uri("pack://application:,,,/ManuallyPopulateTreeView;component/Images/floppy_disk.png"));
                }
                else if (drive.DriveType == DriveType.CDRom)
                {
                    //item.SelectedImage = item.UnselectedImage = new BitmapImage(new Uri("pack://application:,,/Images/cd_drive.png"));
                    item.SelectedImage = item.UnselectedImage = new BitmapImage(new Uri("pack://application:,,,/ManuallyPopulateTreeView;component/Images/cd_drive.png"));
                }
                else
                {
                    //item.SelectedImage = item.UnselectedImage = new BitmapImage(new Uri("pack://application:,,/Images/drive.png"));
                    item.SelectedImage = item.UnselectedImage = new BitmapImage(new Uri("pack://application:,,,/ManuallyPopulateTreeView;component/Images/drive.png"));
                }

                this.Items.Add(item);

                if (chDrive != 'A' && chDrive != 'B' && drive.IsReady)
                {
                    item.Populate();
                }
            }

            this.EndInit();
        }
Ejemplo n.º 3
0
        void TreeViewOnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs <object> args)
        {
            DirectoryTreeViewItem item = args.NewValue as DirectoryTreeViewItem;

            stack.Children.Clear();

            FileInfo[] infos;

            try
            {
                infos = item.DirectoryInfo.GetFiles();
            }
            catch
            {
                return;
            }

            foreach (FileInfo info in infos)
            {
                TextBlock text = new TextBlock();
                text.Text = info.Name;
                stack.Children.Add(text);
            }
        }