English 中文(简体)
WPF TreeView binding through XAML
原标题:

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 http://img23.imageshack.us/img23/9337/treexq.png

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.

最佳回答

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

问题回答

暂无回答




相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签