2011-01-31 1 views
0

현재 내 프로젝트에서 내 프로그램으로 읽고 싶은 파일 카탈로그가 있습니다. 구조체.프로젝트 폴더 구조의 Wpf Treeview 바인드베이스

Project 
    - Properties 
    - References 
    - Manufacturers (want to project this as treeview) 
     - Honda 
     - file1 
     - file2 
     - Toyota 
     - file1 
     - file2 

내 프로그램에서 위의 각 파일은 자체 비즈니스 개체로 표시됩니다. 내 프로그램에서 내 treeview에이 작업을 수행 할 수 있기를 원합니다.

- Honda 
    - file1 
    - file2 
- Toyota 
    - file1 
    - file2 

이 파일을 한 번만로드하므로이 파일을 한 번 읽고 트리보기에 바인딩합니다. 이것을하는 우아한 방법 있는가 ???

감사합니다, 케빈

답변

1

당신은 그런 다음 DataContext 설정 것 HierarchicalDataTemplate

<toolkit:HierarchicalDataTemplate x:Key="FileTemplate" > 
     <TextBlock Text="{Binding Path=FileName}" /> 
</toolkit:HierarchicalDataTemplate> 
<toolkit:HierarchicalDataTemplate x:Key="ManufacturerTemplate" 
     ItemsSource="{Binding Path=Files}" 
     ItemTemplate="{StaticResource FileTemplate}"> 
     <TextBlock Text="{Binding Path=Name}"/> 
</toolkit:HierarchicalDataTemplate> 

<toolkit:TreeView ItemsSource="{Binding}" 
    ItemTemplate="{StaticResource ManufacturerTemplate}"/> 

과 같이 보일 수 귀하의 비즈니스 오브젝트

...

class Manufacturer 
{ 
    String Name {get; set;} 
    ObservableCollection<File> Files {get; set;} 

} 

class File 
{ 
    String FileName {get; set;} 
} 

사용 할 수 있습니다 TreeView ~ ObservableCollection<Manufacturer>

+0

고마워, 난 그냥 질문이, FileTemplate 사용되는 두 번째로,이 내 폴더 이름을 "제조 업체"내 비즈니스 개체를 저장할 필요가 뜻입니까? – Kev84

+0

FileTemplate과 관련하여 @ Kev84 Typo가 수정되었습니다 ... 샘플 비즈니스 객체 –

+0

이 추가되었습니다. 제조 업체 비즈니스 객체를 추가하지 않고도이 작업을 수행 할 수 있으며 File과 PathName을 사용하면됩니다. – Kev84