Click here to Skip to main content
15,891,184 members
Articles / Desktop Programming / WPF

A Versatile TreeView for WPF

Rate me:
Please Sign up or sign in to vote.
4.95/5 (107 votes)
7 Feb 2008CPOL15 min read 641K   18.6K   303  
A strongly typed enhancement of the regular WPF TreeView control.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using SampleShop.Products;

namespace SampleShop
{
  public static class ShopUtil
  {
    public static ObservableCollection<ShopCategory> CreateShop()
    {
      ObservableCollection<ShopCategory> list = new ObservableCollection<ShopCategory>();
      ShopCategory books = new ShopCategory("Books");
      ShopCategory movies = new ShopCategory("Movies");
      ShopCategory music = new ShopCategory("Music");

      //root categories
      list.Add(books);
      list.Add(movies);
      list.Add(music);

      //2nd level items on all categories
      books.SubCategories.Add(new ShopCategory("Fiction", books));
      books.SubCategories.Add(new ShopCategory("Travel", books));
      books.SubCategories.Add(new ShopCategory("Politics", books));

      movies.SubCategories.Add(new ShopCategory("Action", movies));
      movies.SubCategories.Add(new ShopCategory("Fantasy", movies));
      movies.SubCategories.Add(new ShopCategory("Romance", movies));
      movies.SubCategories.Add(new ShopCategory("Horror", movies));

      music.SubCategories.Add(new ShopCategory("Pop", music));
      music.SubCategories.Add(new ShopCategory("Techno", music));
      music.SubCategories.Add(new ShopCategory("Classical", music));
      music.SubCategories.Add(new ShopCategory("Ethno", music));
      ShopCategory rock = new ShopCategory("Rock", music);
      music.SubCategories.Add(rock);      
      
      //get 3rd level on rock
      rock.SubCategories.Add(new ShopCategory("Alternative", rock));
      rock.SubCategories.Add(new ShopCategory("Metal", rock));
      rock.SubCategories.Add(new ShopCategory("Industrial Rock", rock));

      

      return list;
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect I'm a gun for hire
Switzerland Switzerland
Philipp is an independent software engineer with great love for all things .NET.
He lives in Winterthur, Switzerland and his home on the web is at http://www.hardcodet.net.

Comments and Discussions