첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ TreeView 클래스 : 템플리트를 사용해 트리 만들기

----------------------------------------------------------------------------------------------------


TestProject.zip


DiskDirectory.cs

 

 

using System.Collections.Generic;

using System.IO;

 

namespace TestProject

{

    /// <summary>

    /// 디스크 디렉토리

    /// </summary>

    public class DiskDirectory

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 디렉토리 정보

        /// </summary>

        private DirectoryInfo directoryInfo;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 명칭 - Name

 

        /// <summary>

        /// 명칭

        /// </summary>

        public string Name

        {

            get

            {

                return this.directoryInfo.Name;

            }

        }

 

        #endregion

        #region 하위 디스크 디렉토리 리스트 - SubsidaryDiskDirectoryList

 

        /// <summary>

        /// 하위 디스크 디렉토리 리스트

        /// </summary>

        public List<DiskDirectory> SubsidaryDiskDirectoryList

        {

            get

            {

                List<DiskDirectory> diskDirectoryList = new List<DiskDirectory>();

 

                DirectoryInfo[] subsidaryDirectoryInfoArray;

 

                try

                {

                    subsidaryDirectoryInfoArray = this.directoryInfo.GetDirectories();

                }

                catch

                {

                    return diskDirectoryList;

                }

 

                foreach(DirectoryInfo subsidaryDirectoryInfo in subsidaryDirectoryInfoArray)

                {

                    diskDirectoryList.Add(new DiskDirectory(subsidaryDirectoryInfo));

                }

 

                return diskDirectoryList;

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - DiskDirectory(directoryInfo)

 

        /// <summary>

        /// 생성자

        /// </summary>

        /// <param name="directoryInfo">디렉토리 정보</param>

        public DiskDirectory(DirectoryInfo directoryInfo)

        {

            this.directoryInfo = directoryInfo;

        }

 

        #endregion

    }

}

 

 

MainWindow.cs

 

 

using System;

using System.IO;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Media;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            Width      = 800;

            Height     = 600;

            Title      = "TreeView 클래스 : 템플리트를 사용해 트리 만들기";

            FontFamily = new FontFamily("나눔고딕코딩");

            FontSize   = 16;

 

            TreeView treeView = new TreeView();

 

            Content = treeView;

 

            HierarchicalDataTemplate hierarchicalDataTemplate = new HierarchicalDataTemplate(typeof(DiskDirectory));

 

            hierarchicalDataTemplate.ItemsSource = new Binding("SubsidaryDiskDirectoryList");

 

            FrameworkElementFactory textBlockFrameworkElementFactory = new FrameworkElementFactory(typeof(TextBlock));

 

            textBlockFrameworkElementFactory.SetBinding(TextBlock.TextProperty, new Binding("Name"));

 

            hierarchicalDataTemplate.VisualTree = textBlockFrameworkElementFactory;

 

            DiskDirectory diskDirectory = new DiskDirectory(new DirectoryInfo("c:\\Windows"));

 

            TreeViewItem treeViewItem = new TreeViewItem();

 

            treeViewItem.Header       = diskDirectory.Name;

            treeViewItem.ItemsSource  = diskDirectory.SubsidaryDiskDirectoryList;

            treeViewItem.ItemTemplate = hierarchicalDataTemplate;

 

            treeView.Items.Add(treeViewItem);

 

            treeViewItem.IsExpanded = true;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Public

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        [STAThread]

        public static void Main()

        {

            Application application = new Application();

 

            application.Run(new MainWindow());

        }

 

        #endregion

    }

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요