データがコレクション内に整理されている場合は、HierarchicalDataTemplatesを使用してTreeViewを作成できます。しかし、いくつかのよく知られているクラスに保存されているいくつかのデータはTreeViewに表示する必要があります。ここでは、コードの例です:コレクションのない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());
}
}
そしてここでは、XAMLコードは次のとおりです。
<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は、テンプレート全体を1つの項目として扱うようです。ルートノードとサブツリー「GroupWith2Children」全体のみを選択できます。
GroupWith2Childrenをコレクションに置き換えることは私の選択肢ではありません。どんなアイデア、何が間違っているの?
作品を、しかし、私は(リストと名への参照を持っている)のIEnumerableを表示するために使用する必要がありますハードコードされた名前( "Group1"と "Group2")。ありがとう –
Paul