我試圖使用數據模板將集合綁定到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