So I have a class structure like this
Store
Owner
Cashier
List of Products
Product Name
Price
List of Vendors
Company Name
Contact Name
So, there are 4 objects: Store, Person (Owner and Cashier), Product and Supplier.
I want to find a way to bind this structure to the XAML tree, so each node represents an object of this structure, for example:
image http://img23.imageshack.us/img23/9337/treexq.png
So far, I could only use one top object -> one subobject. Like this:
Store
- Departmen 1 Products
Book 1
Book 2
Book 3
- Department 2 Products
Bike 1
Bike 2
Bike 3
Thus, the important difference here is that in the first tree, the subnodes are 3 different types of objects (Store has 2 Person objects, 1 Product and 1 Vendor objects); and in the second, each root of a node has only one type of sub node (the store has a department, the department has products).
, HierarchicalDataTemplates, , , . , ? , Store:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.DataContext = new List<Store>() { Store.CreateStore() };
}
}
public class Store
{
public string StoreName { get; set; }
public Person Owner { get; set; }
public Person Cashier { get; set; }
public List<Product> Products { get; set; }
public List<Vendor> Vendors { get; set; }
public Store()
{
this.Products = new List<Product>();
this.Vendors = new List<Vendor>();
}
public static Store CreateStore()
{
Store store = new Store();
store.StoreName = "Book store";
store.Owner = new Person() { FirstName = "John", LastName = "Smith" };
store.Cashier = new Person() { FirstName = "Jane", LastName = "Smart" };
store.Products.Add(new Product() { Name = "Mechanical Pencil", Price = 1.25m});
store.Products.Add(new Product() { Name = "Pen", Price = 2.50m });
store.Products.Add(new Product() { Name = "WPF Book", Price = 28.94m });
store.Products.Add(new Product() { Name = "ASP.NET Book", Price = 29.50m });
store.Vendors.Add(new Vendor() { CompanyName = "Bic", ContactName = "Bill Gates" });
store.Vendors.Add(new Vendor() { CompanyName = "O'Reilly", ContactName = "Steve Jobs" });
return store;
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Product
{
public string Name { get; set; }
public decimal Price { get; set; }
}
public class Vendor
{
public string CompanyName { get; set; }
public string ContactName { get; set; }
}
. .