WPF简单的文件资源管理
![](http://www.silverlightchina.net/uploads/allimg/130226/2311422233-0.jpg)
<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;
}
}
}
}
本文来自Leon zhang博客,原文地址:http://www.cnblogs.com/zhangliangzlee/archive/2013/02/27/2934866.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!