데이터가 컬렉션으로 구성되어 있으면 HierarchicalDataTemplates를 사용하여 TreeView를 채울 수 있습니다. 하지만 몇 가지 잘 알려진 클래스에 저장된 데이터가 TreeView에 표시해야합니다. XAML 코드 여기컬렉션이없는 TreeView 재귀
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
namespace Test
{
public class GroupWithList
{
public string Name { get; set; }
public ObservableCollection<string> Items { get; set; }
public GroupWithList(string name)
{
this.Name = name;
this.Items = new ObservableCollection<string>();
}
}
public class GroupWith2Children
{
public string Name { get; set; }
public ObservableCollection<GroupWithList> Groups { get; set; }
public GroupWithList First
{
get
{
return this.Groups[0];
}
}
public GroupWithList Second
{
get
{
return this.Groups[1];
}
}
public GroupWith2Children()
{
this.Name = "GroupWith2Children";
this.Groups = new ObservableCollection<GroupWithList>();
GroupWithList g;
g = new GroupWithList("Letters");
g.Items.Add("u");
g.Items.Add("a");
g.Items.Add("t");
this.Groups.Add(g);
g = new GroupWithList("Numbers");
g.Items.Add("12");
g.Items.Add("153");
this.Groups.Add(g);
}
}
}
public partial class MainWindow : Window
{
public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(ObservableCollection<GroupWith2Children>), typeof(MainWindow), new FrameworkPropertyMetadata(new ObservableCollection<GroupWith2Children>()));
public ObservableCollection<GroupWith2Children> Items
{
get
{
return (ObservableCollection<GroupWith2Children>)this.GetValue(ItemsProperty);
}
set
{
this.SetValue(ItemsProperty, value);
}
}
public MainWindow()
{
InitializeComponent();
this.Items.Add(new GroupWith2Children());
this.Items.Add(new GroupWith2Children());
}
}
그리고있다 :
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:test="clr-namespace:Test"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<DataTemplate DataType="{x:Type test:GroupWith2Children}">
<TreeViewItem Header="{Binding Mode=OneWay, Path=Name}">
<TreeViewItem Header="First" ItemsSource="{Binding Mode=OneWay, Path=First.Items}"/>
<TreeViewItem Header="Second" ItemsSource="{Binding Mode=OneWay, Path=Second.Items}"/>
</TreeViewItem>
</DataTemplate>
<HierarchicalDataTemplate DataType="{x:Type test:GroupWithList}" ItemsSource="{Binding Path=Items}">
<TextBlock Text="GroupWithList"/>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type sys:String}">
<TextBlock Text="{Binding}"/>
</DataTemplate>
</Grid.Resources>
<TreeView>
<TreeViewItem Header="Root" ItemsSource="{Binding Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=Items}"/>
</TreeView>
</Grid>
</Window>
예는 잘 작동하지만 당신이 GroupWith2Children의 DataTemplate을 생성하는 항목을 선택할 수 없습니다 다음 코드 예입니다. TreeView는 전체 템플릿을 하나의 항목으로 취급합니다. 루트 노드와 전체 하위 트리 "GroupWith2Children"만 선택할 수 있습니다.
GroupWith2Children을 모음으로 바꾸는 것은 나를위한 옵션이 아닙니다. 어떤 생각, 내가 뭘 잘못하고있어?
작품,하지만 난 (목록 및 이름에 대한 참조가)는 IEnumerable가를 표시하는 데 사용할 필요가 하드 코딩 된 이름 ("Group1"및 "Group2"). 감사 –
Paul