• WPF简单的文件资源管理


      

    <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

  • 相关阅读:
    读取lotus85邮箱未读文档
    Lotus notes 通讯录的导入导出
    如果你发现vpuserinfo.nsf数据库损坏,如何修复?该数据
    如何从iNotes获得未读邮件数量
    NTKO文档控件常见报错信息集合
    页面缓存清除的方法
    通过Lotusscript代码从损坏的数据库中抽取数据
    如何将lotus 通讯簿导入到outlook 2003中
    怎样使用Lotus Domino实用程序(Updall, Compact, Fixup) 进行正常和异常维护。
    0301.Lotus Domino与Windows AD帐户同步和SSO
  • 原文地址:https://www.cnblogs.com/weikai/p/3125504.html
Copyright © 2020-2023  润新知