728x90
반응형
728x170
■ TreeViewItem 클래스의 ItemsSource 속성을 사용해 트리 뷰를 만드는 방법을 보여준다.
▶ 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
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] MessageBox 클래스 : 메시지 박스 사용하기 (0) | 2017.06.11 |
---|---|
[C#/WPF] DataGrid 클래스 : ADO.NET 엔터티 데이터 모델 바인딩 하기 (0) | 2017.06.11 |
[C#/WPF] 애니메이션 버튼 사용하기 (0) | 2017.06.11 |
[C#/WPF] DesignInstance 확장 태그 사용하기 (0) | 2017.06.11 |
[C#/WPF] ObservableCollection<T> 클래스 : 바인딩 설정하기 (0) | 2017.06.11 |
[C#/WPF] NavigationWindow 엘리먼트 : 페이지 탐색하기 (0) | 2017.06.11 |
[C#/WPF] XAML용 UI 디버깅 도구 숨기기 (0) | 2017.05.30 |
[C#/WPF] ControlTemplate 엘리먼트 : 조직도 TreeViewItem 엘리먼트 정의하기 (0) | 2017.05.25 |
[C#/WPF] 컬럼 리스트 박스 사용하기 (0) | 2017.05.09 |
[C#/WPF] ItemsControl 클래스 : 리스트 컨트롤 사용하기 (0) | 2017.04.07 |
댓글을 달아 주세요