첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ TreeViewItem 클래스의 ItemsSource 속성을 사용해 트리 뷰를 만드는 방법을 보여준다.

TestProject.zip
다운로드

▶ Folder.cs

using System;
using System.IO;
using System.Collections.ObjectModel;

namespace TestProject
{
    /// <summary>
    /// 폴더
    /// </summary>
    public class Folder
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 디렉토리 정보
        /// </summary>
        private DirectoryInfo directoryInfo;

        /// <summary>
        /// 하위 폴더 컬렉션
        /// </summary>
        private ObservableCollection<Folder> subFolderCollection;

        /// <summary>
        /// 파일 정보 컬렉션
        /// </summary>
        private ObservableCollection<FileInfo> fileInfoCollection;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 완전한 경로 - FullPath

        /// <summary>
        /// 완전한 경로
        /// </summary>
        public string FullPath
        {
            get
            {
                return this.directoryInfo.FullName;
            }

            set
            {
                if(Directory.Exists(value))
                {
                    this.directoryInfo = new DirectoryInfo(value);
                }
                else
                {
                    throw new ArgumentException("경로가 없습니다.", "FullPath");
                }
            }
        }

        #endregion
        #region 명칭 - Name

        /// <summary>
        /// 명칭
        /// </summary>
        public string Name
        {
            get
            {
                return this.directoryInfo.Name;
            }
        }

        #endregion
        #region 하위 폴더 컬렉션 - SubFolderCollection

        /// <summary>
        /// 하위 폴더 컬렉션
        /// </summary>
        public ObservableCollection<Folder> SubFolderCollection
        {
            get
            {
                if(this.subFolderCollection == null)
                {
                    this.subFolderCollection = new ObservableCollection<Folder>();

                    DirectoryInfo[] directoryInfoArray = this.directoryInfo.GetDirectories();

                    for(int i = 0; i < directoryInfoArray.Length; i++)
                    {
                        Folder folder = new Folder();

                        folder.FullPath = directoryInfoArray[i].FullName;

                        this.subFolderCollection.Add(folder);
                    }
                }

                return this.subFolderCollection;
            }
        }

        #endregion
        #region 파일 정보 컬렉션 - FileInfoCollection

        /// <summary>
        /// 파일 정보 컬렉션
        /// </summary>
        public ObservableCollection<FileInfo> FileInfoCollection
        {
            get
            {
                if(this.fileInfoCollection == null)
                {
                    this.fileInfoCollection = new ObservableCollection<FileInfo>();

                    FileInfo[] fileInfoArray = this.directoryInfo.GetFiles();

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

                return this.fileInfoCollection;
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - Folder()

        /// <summary>
        /// 생성자
        /// </summary>
        public Folder()
        {
            FullPath = @"c:\";
        }

        #endregion
    }
}

 

▶ MainWindow.xaml

<Window
    x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestProject"
    Width="800"
    Height="600"
    Title="폴더 탐색"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Window.Resources>
        <ObjectDataProvider x:Key="ObjectDataProviderKey">
            <ObjectDataProvider.ObjectInstance>
                <local:Folder FullPath="c:\" />
            </ObjectDataProvider.ObjectInstance>
        </ObjectDataProvider>
        <HierarchicalDataTemplate
            DataType="{x:Type local:Folder}"
            ItemsSource="{Binding Path=SubFolderCollection}">
            <TextBlock Text="{Binding Path=Name}" />
        </HierarchicalDataTemplate>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition           />
            <ColumnDefinition Width="2*"/>
        </Grid.ColumnDefinitions>
        <TreeView Name="folderTreeView" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2">
            <TreeViewItem
                Header="폴더"
                ItemsSource="{Binding Source={StaticResource ObjectDataProviderKey}, Path=SubFolderCollection}" />
        </TreeView>
        <ListView Grid.Row="0" Grid.Column="1"
            ItemsSource="{Binding ElementName=folderTreeView, Path=SelectedItem.SubFolderCollection, Mode=OneWay}" />
        <ListView Grid.Row="1" Grid.Column="1"
            ItemsSource="{Binding ElementName=folderTreeView, Path=SelectedItem.FileInfoCollection, Mode=OneWay}" />
    </Grid>
</Window>
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요