Click here to Skip to main content
15,885,366 members
Articles / Desktop Programming / WPF

Custom TreeView Layout in WPF

Rate me:
Please Sign up or sign in to vote.
4.90/5 (66 votes)
24 Jan 2007CPOL3 min read 575.9K   11.5K   230  
Shows how to turn a TreeView into an Org Chart.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CustomTreeViewLayout
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        void OnLoaded( object sender, RoutedEventArgs e )
        {
            // Prevent the TreeView from responding to the keyboard.
            // Since there's no sane way to set a TreeView's SelectedItem,
            // we can't customize the keyboard navigation logic. :(
            this.tree.PreviewKeyDown += delegate( object obj, KeyEventArgs args ) { args.Handled = true; };

            // Put some data into the TreeView.
            this.PopulateTreeView();
        }

        void PopulateTreeView()
        {
            Node root = new Node( "Big Daddy Root" );

            int branches = 0;
            int subBranches = 0;

            for( int i = 0; i < 2; ++i )
            {
                Node child = new Node( "Branch " + ++branches );
                root.ChildNodes.Add( child );

                for( int j = 0; j < 3; ++j )
                {
                    Node gchild = new Node( "Sub-Branch " + ++subBranches );
                    child.ChildNodes.Add( gchild );

                    for( int k = 0; k < 2; ++k )
                        gchild.ChildNodes.Add( new Node( "Leaf" ) );
                }
            }

            // Create a dummy node so that we can bind the TreeView
            // it's ChildNodes collection.
            Node dummy = new Node();
            dummy.ChildNodes.Add( root );

            this.tree.ItemsSource = dummy.ChildNodes;
        }
    }
}

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
Software Developer (Senior)
United States United States
Josh creates software, for iOS and Windows.

He works at Black Pixel as a Senior Developer.

Read his iOS Programming for .NET Developers[^] book to learn how to write iPhone and iPad apps by leveraging your existing .NET skills.

Use his Master WPF[^] app on your iPhone to sharpen your WPF skills on the go.

Check out his Advanced MVVM[^] book.

Visit his WPF blog[^] or stop by his iOS blog[^].

See his website Josh Smith Digital[^].

Comments and Discussions