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

Basic Understanding of Tree View in WPF

Rate me:
Please Sign up or sign in to vote.
4.90/5 (114 votes)
3 Nov 2010CPOL6 min read 486.9K   16.9K   150  
This article explains different ways to show contents in the Tree View control.
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;
using WpfTreeViewExamples.Controls;

namespace WpfTreeViewExamples
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        #region Constructor

        public MainWindow()
        {
            InitializeComponent();
        }

        #endregion

        #region Private Methods

        private void ShowControl(Control control, string title)
        {

            control.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            control.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;

            control.Width = double.NaN;
            control.Height = double.NaN;

            gvContainer.Children.Clear();
            gvContainer.Children.Add(control);


            lblTitle.Content = title;
        }

        #endregion

        #region Events

        private void btnSimple_Click(object sender, RoutedEventArgs e)
        {
            ShowControl(new ucSimpleTree(), "Simple Tree");
        }

        private void btnSimpleCodeBehind_Click(object sender, RoutedEventArgs e)
        {
            ShowControl(new ucSimpleTreeCodeBehind(), "Simple Tree using code behind");
        }

        private void btnCustomByXaml_Click(object sender, RoutedEventArgs e)
        {
            ShowControl(new ucCustomTree(), "Custom Tree");
        }

        private void btnCustomByCode_Click(object sender, RoutedEventArgs e)
        {
            ShowControl(new ucCustomTreeCodeBehind(), "Custom Tree using code behind");
        }

        private void btnCustomByInheritance_Click(object sender, RoutedEventArgs e)
        {

            ShowControl(new ucCustomTreeCustomClass(), "Custom Tree by custom class");
        }

        private void btnHeaderTemplateByXaml_Click(object sender, RoutedEventArgs e)
        {
            ShowControl(new ucTreeHeaderTemplate(), "Create Header Template");
        }

        private void btnHeaderTemplateByCode_Click(object sender, RoutedEventArgs e)
        {
            ShowControl(new ucTreeHeaderTemplateCodeBehind(), "Create Header Template from code behind");
        }

        private void btnBindingAddSimpleObject_Click(object sender, RoutedEventArgs e)
        {
            ShowControl(new ucAddSimpleObject(), "Add Simple object");
        }

        private void btnBindingAddSimpleObjectUsingCode_Click(object sender, RoutedEventArgs e)
        {
            ShowControl(new ucAddSimpleObjectUsingCodeBehind (), "Add Simple object from code behind");
        }

        private void btnDataBinding_Click(object sender, RoutedEventArgs e)
        {
            ShowControl(new ucDataBinding(), "Data Binding");
        }

        private void btnDataBindingUsingCode_Click(object sender, RoutedEventArgs e)
        {
            ShowControl(new ucDataBindCodeBehind(), "Data Binding using code behind");
        }

        private void btnDataTemplate_Click(object sender, RoutedEventArgs e)
        {
            ShowControl(new ucDataTemplate(), "Data Template");
        }
       
        private void btnHierarchicalDataTemplate_Click(object sender, RoutedEventArgs e)
        {
            ShowControl(new ucHierarchicalDataTemplate(), "Hierarchical Data Template");
        }

        #endregion
        
    }
}







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
Chief Technology Officer
Pakistan Pakistan
Passion and positive dedication is essential part of success. I believe on hardworking and sharing knowledge with others. I always try to be a better than I am and think positive for positive result.

My Blogs

My Linked-In Profile

Comments and Discussions