我试图使用数据模板将集合绑定到wpf TreeView
控件。集合中的每个项目(人员)还包含两种不同类型的汽车和书籍的集合(汽车,书籍)。WPF TreeView HierarchicalDataTemplate - 绑定到具有不同子集合的对象
下面是节省空间所涉及对象的简化列表。
public class Person
{
public string Name
public List<Book> Books;
public List<Car> Cars;
}
public class Book
{
public string Title
public string Author
}
public class Car
{
public string Manufacturer;
public string Model;
}
这里是我如何结合
public MainWindow()
{
InitializeComponent();
this.treeView1.ItemsSource = this.PersonList();
}
public List<Person> PersonList()
{
List<Person> list = new List<Person>();
Book eco = new Book { Title = "Economics 101", Author = "Adam Smith"};
Book design = new Book { Title = "Web Design", Author = "Robins" };
Car corola = new Car { Manufacturer = "Toyota", Model = "2005 Corola"};
Car ford = new Car { Manufacturer = "Ford", Model = "2008 Focus"};
Person john = new Person { Name = "John", Books = new ObservableCollection<Book> { eco, design }, Cars = new ObservableCollection<Car> { corola } };
Person smith = new Person { Name = "Smith", Books = new ObservableCollection<Book> { eco, design }, Cars = new ObservableCollection<Car> { ford } };
list.AddRange(new[] {john, smith });
return list;
}
这里是XAML代码
<Grid>
<TreeView Name="treeView1">
</TreeView>
</Grid>
我期待看到树的显示这个样子。
>John
>Books
Economics 101 : Adam Smith
Web Design : Robins
>Cars
Totota : 2005 Corola
>Smith
>Books
Economics 101 : Adam Smith
Web Design : Robins
>Cars
Ford: 2008 Focus
此标志>
用于显示树文件夹,不应在模板中考虑。
你可以帮助我与温度设计? – Jama 2010-02-19 19:27:24
约翰 书籍 经济学101:亚当·斯密 网页设计:罗宾斯 汽车 托托塔:2005 Corola 史密斯 书籍 经济学101:亚当·斯密 网页设计:罗宾斯 汽车 福特:2008年福克斯 – Jama 2010-02-19 19:29:22