0
我并不都熟悉标记。我试图用TreeView
来测试DataTemplate
。基本上我试图创建一个名为parents
的列表,向其中添加2个元素,然后创建两个子项并将它们添加到列表中的父项之一,最后将父列表绑定到TreeView
。但我不知道如何引用p1, p2
里面的列表。现在我想我不应该用xaml浪费我的时间,并且应该在代码背后做到这一点。有任何想法吗?如何使用子集合制作父元素集合
<Window.Resources>
<local:Parent Name="Parent 1" x:Key="p1"/>
<local:Parent Name="Parent 2" x:Key="p2"/>
<x:ArrayExtension Type="local:Parent" x:Key="parents">
<!-- Can't refer p1 and p2 from here :(-->
</x:ArrayExtension>
<local:Child ChildName="Child 1" Parent="{StaticResource ResourceKey=p1}"/>
<local:Child ChildName="Child 1" Parent="{StaticResource ResourceKey=p1}"/>
</Window.Resources>
<Grid>
<StackPanel VerticalAlignment="Center">
<TreeView ItemsSource="{Binding Source={StaticResource ResourceKey=p1}}" />
</StackPanel>
</Grid>
这两个类:
public class Parent
{
public string Name { get; set; }
public IList<Child> Children { get; set; }
public Parent()
{
Children = new List<Child>();
}
}
public class Child
{
public string ChildName { get; set; }
private Parent _parent;
public Parent Parent
{
get
{
return _parent;
}
set
{
_parent = value;
if (value != null)
{
value.Children.Add(this);
}
}
}
}