첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
0.02MB

▶ ExplorerItem.cs

using System.Collections.ObjectModel;
using System.ComponentModel;

namespace TestProject
{
    /// <summary>
    /// 탐색기 항목
    /// </summary>
    public class ExplorerItem : INotifyPropertyChanged
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Enumeration
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 탐색기 항목 타입 - ExplorerItemType

        /// <summary>
        /// 탐색기 항목 타입
        /// </summary>
        public enum ExplorerItemType
        {
            Folder,
            File
        };

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Event
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 속성 변경시 - PropertyChanged

        /// <summary>
        /// 속성 변경시
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 자식 컬렉션
        /// </summary>
        private ObservableCollection<ExplorerItem> children;

        /// <summary>
        /// 확장 여부
        /// </summary>
        private bool isExpanded;

        /// <summary>
        /// 선택 여부
        /// </summary>
        private bool isSelected;

        #endregion

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

        #region 명칭 - Name

        /// <summary>
        /// 명칭
        /// </summary>
        public string Name { get; set; }

        #endregion
        #region 타입 - Type

        /// <summary>
        /// 타입
        /// </summary>
        public ExplorerItemType Type { get; set; }

        #endregion
        #region 자식 컬렉션 - Children

        /// <summary>
        /// 자식 컬렉션
        /// </summary>
        public ObservableCollection<ExplorerItem> Children
        {
            get
            {
                if(this.children == null)
                {
                    this.children = new ObservableCollection<ExplorerItem>();
                }

                return this.children;
            }
            set
            {
                this.children = value;
            }
        }

        #endregion
        #region 확장 여부 - IsExpanded

        /// <summary>
        /// 확장 여부
        /// </summary>
        public bool IsExpanded
        {
            get
            {
                return this.isExpanded;
            }
            set
            {
                if(this.isExpanded != value)
                {
                    this.isExpanded = value;

                    FirePropertyChangedEvent("IsExpanded");
                }
            }
        }

        #endregion
        #region 선택 여부 - IsSelected

        /// <summary>
        /// 선택 여부
        /// </summary>
        public bool IsSelected
        {
            get
            {
                return this.isSelected;
            }
            set
            {
                if(this.isSelected != value)
                {
                    this.isSelected = value;

                    FirePropertyChangedEvent("IsSelected");
                }
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 속성 변경시 이벤트 발생시키기 - FirePropertyChangedEvent(propertyName)

        /// <summary>
        /// 속성 변경시 이벤트 발생시키기
        /// </summary>
        /// <param name="propertyName">속성명</param>
        private void FirePropertyChangedEvent(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}

 

728x90

 

▶ MainPage.xaml

<Page x:Class="TestProject.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
    xmlns:local="using:TestProject"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <Border
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            BorderThickness="1"
            BorderBrush="{ThemeResource TextControlBorderBrush}">
            <muxc:TreeView x:Name="treeView"
                Width="400"
                Height="400"
                ItemsSource="{x:Bind explorerItemCollection}">
                <muxc:TreeView.ItemTemplate>
                    <DataTemplate x:DataType="local:ExplorerItem">
                        <muxc:TreeViewItem
                            Content="{x:Bind Name}"
                            ItemsSource="{x:Bind Children}"
                            IsExpanded="True" />
                    </DataTemplate>
                </muxc:TreeView.ItemTemplate>
            </muxc:TreeView>
        </Border>
    </Grid>
</Page>

 

300x250

 

▶ MainPage.xaml.cs

using System.Collections.ObjectModel;
using Windows.Foundation;
using Windows.Graphics.Display;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace TestProject
{
    /// <summary>
    /// 메인 페이지
    /// </summary>
    public sealed partial class MainPage : Page
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 탐색기 항목 컬렉션
        /// </summary>
        private ObservableCollection<ExplorerItem> explorerItemCollection;

        #endregion

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

        #region 생성자 - MainPage()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainPage()
        {
            InitializeComponent();

            #region 윈도우 크기를 설정한다.

            double width  = 800d;
            double height = 600d;

            double dpi = (double)DisplayInformation.GetForCurrentView().LogicalDpi;

            ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;

            Size windowSize = new Size(width * 96d / dpi, height * 96d / dpi);

            ApplicationView.PreferredLaunchViewSize = windowSize;

            Window.Current.Activate();

            ApplicationView.GetForCurrentView().TryResizeView(windowSize);

            #endregion
            #region 윈도우 제목을 설정한다.

            ApplicationView.GetForCurrentView().Title = "TreeView 엘리먼트 : ItemsSource 속성 사용하기";

            #endregion

            this.explorerItemCollection = GetExplorerItemCollection();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 탐색기 항목 컬렉션 구하기 - GetExplorerItemCollection()

        /// <summary>
        /// 탐색기 항목 컬렉션 구하기
        /// </summary>
        /// <returns>탐색기 항목 컬렉션</returns>
        private ObservableCollection<ExplorerItem> GetExplorerItemCollection()
        {
            ObservableCollection<ExplorerItem> collection = new ObservableCollection<ExplorerItem>();

            ExplorerItem item1 = new ExplorerItem()
            {
                Name     = "Work Documents",
                Type     = ExplorerItem.ExplorerItemType.Folder,
                Children =
                {
                    new ExplorerItem()
                    {
                        Name     = "Functional Specifications",
                        Type     = ExplorerItem.ExplorerItemType.Folder,
                        Children =
                        {
                            new ExplorerItem()
                            {
                                Name = "TreeView spec",
                                Type = ExplorerItem.ExplorerItemType.File
                            }
                        }
                    },
                    new ExplorerItem()
                    {
                        Name = "Feature Schedule",
                        Type = ExplorerItem.ExplorerItemType.File
                    },
                    new ExplorerItem()
                    {
                        Name = "Overall Project Plan",
                        Type = ExplorerItem.ExplorerItemType.File
                    },
                    new ExplorerItem()
                    {
                        Name = "Feature Resources Allocation",
                        Type = ExplorerItem.ExplorerItemType.File
                    }
                }
            };

            ExplorerItem item2 = new ExplorerItem()
            {
                Name     = "Personal Folder",
                Type     = ExplorerItem.ExplorerItemType.Folder,
                Children =
                {
                    new ExplorerItem()
                    {
                        Name    = "Home Remodel Folder",
                        Type    = ExplorerItem.ExplorerItemType.Folder,
                        Children =
                        {
                            new ExplorerItem()
                            {
                                Name = "Contractor Contact Info",
                                Type = ExplorerItem.ExplorerItemType.File
                            },
                            new ExplorerItem()
                            {
                                Name = "Paint Color Scheme",
                                Type = ExplorerItem.ExplorerItemType.File
                            },
                            new ExplorerItem()
                            {
                                Name = "Flooring Woodgrain type",
                                Type = ExplorerItem.ExplorerItemType.File
                            },
                            new ExplorerItem()
                            {
                                Name = "Kitchen Cabinet Style",
                                Type = ExplorerItem.ExplorerItemType.File
                            }
                        }
                    }
                }
            };

            collection.Add(item1);
            collection.Add(item2);

            return collection;
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요