views:

187

answers:

1

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 Vendor.

I want to find a way to bind this structure into a XAML tree, so each node is represents an object of this structure like:

image

Up until now, I've only been able to this with one top object -> one sub object. Like this:

Store
   - Departmen 1 Products
       Book 1
       Book 2
       Book 3
   - Department 2 Products
       Bike 1
       Bike 2
       Bike 3

So the important difference here is that in the first tree, the sub nodes are 3 different types of object (Store has 2 Person objects, 1 Product and 1 Vendor objects); and in the second one, each root node has only one type of sub node (Store has Department, Department has Products).

I've done the second example using HierarchicalDataTemplates, so I thought this would solve my problem, but it doesn't. Any Idea on how I can do this? Here's the code that creates the Store structure:

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();

  // set name
  store.StoreName = "Book store";

  // set staff
  store.Owner = new Person() { FirstName = "John", LastName = "Smith" };
  store.Cashier = new Person() { FirstName = "Jane", LastName = "Smart" };

  // add products
  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 });

  // add vendors
  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; }
}

I want to avoid creating the tree structure in the code side. Any help will be GREATLY appreciated.

+1  A: 

I found a great post Organizing Heterogeneous Data on a WPF TreeView that solved this problem using MultiBinding to combine the different collections and objects together, and a MultiValueConverter to "shape" the tree of items. Solved a lot of my problems, but still working on styling it to my taste.

Hope this helps

Nigel Sampson
Ok this looks like it's exactly what I need. I'll get back to you. Thanks!!
Carlo
Finally had time to test this. Works like a charm, thank you!
Carlo