现在的位置: 首页 > 综合 > 正文

WPF简单的文件资源管理

2012年08月10日 ⁄ 综合 ⁄ 共 2647字 ⁄ 字号 评论关闭

<Window x:Class="WPFFolderExplorer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:WPFFolderExplorer"
        Title="Folder Explorer" Height="350" Width="525">
    <Window.Resources>
        <!--ObjectDataProvider:包装和创建可以用作绑定源的对象。-->
        <ObjectDataProvider x:Key="RootFolderDataProvider">
            <!--ObjectInstance:对象实例-->
            <ObjectDataProvider.ObjectInstance>
                <!--FullPath:是Folder类的一个属性-->
                <my:Folder FullPath="c:\Program Files\"/>
            </ObjectDataProvider.ObjectInstance>
        </ObjectDataProvider>
        <!--HierarchicalDataTemplate:分层数据模板-->
        <HierarchicalDataTemplate DataType="{x:Type my:Folder}" ItemsSource="{Binding Path=SubFolders}">
            <TextBlock Text="{Binding Path=Name}"></TextBlock>
        </HierarchicalDataTemplate>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="2*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <TreeView Grid.RowSpan="2"  Grid.ColumnSpan="1" Height="auto" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" Width="auto" Name="treeView1">
            <TreeViewItem ItemsSource="{Binding Path=SubFolders, Source={ StaticResource RootFolderDataProvider}}" Header="文件"/>
        </TreeView>
        <ListView Name="listView1" 
        ItemsSource="{Binding Path=SelectedItem.SubFolders, ElementName=treeView1, Mode=OneWay}" 
        Grid.Column="1" 
        Grid.RowSpan="1" />

        <ListView Name="listView2" 
        ItemsSource="{Binding Path=SelectedItem.Files, ElementName=treeView1, Mode=OneWay}" 
        Grid.Column="1" 
        Grid.Row="1" />
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections.ObjectModel;

namespace WPFFolderExplorer
{
    public class Folder
    {
        private DirectoryInfo _folder;
        private ObservableCollection<Folder> _subFolders;
        private ObservableCollection<FileInfo> _files;

        public Folder()
        {
            this.FullPath = @"c:\Program Files\";
        }

        public string Name
        {
            get
            {
                return this._folder.Name;
            }
        }

        public string FullPath
        {
            get
            {
                return this._folder.FullName;
            }

            set
            {
                if (Directory.Exists(value))
                {
                    this._folder = new DirectoryInfo(value);
                }
                else
                {
                    throw new ArgumentException("must exist", "fullPath");
                }
            }
        }

        public ObservableCollection<FileInfo> Files
        {
            get
            {
                if (this._files == null)
                {
                    this._files = new ObservableCollection<FileInfo>();

                    FileInfo[] fi = this._folder.GetFiles();

                    for (int i = 0; i < fi.Length; i++)
                    {
                        this._files.Add(fi[i]);
                    }
                }

                return this._files;
            }
        }

        public ObservableCollection<Folder> SubFolders
        {
            get
            {
                if (this._subFolders == null)
                {
                    this._subFolders = new ObservableCollection<Folder>();

                    DirectoryInfo[] di = this._folder.GetDirectories();

                    for (int i = 0; i < di.Length; i++)
                    {
                        Folder newFolder = new Folder();
                        newFolder.FullPath = di[i].FullName;
                        this._subFolders.Add(newFolder);
                    }
                }

                return this._subFolders;
            }
        }
    }
}

 

抱歉!评论已关闭.