Click here to Skip to main content
15,891,431 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.8K   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.Library;

namespace WpfTreeViewExamples.Controls
{
    /// <summary>
    /// Interaction logic for ucAddSimpleObject.xaml
    /// </summary>
    public partial class ucAddSimpleObject : UserControl
    {

        #region Constructor

        public ucAddSimpleObject()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            FillTree();
        }

        private void btnShow_Click(object sender, RoutedEventArgs e)
        {
            List<CheckBox> list = new List<CheckBox>();
            list = GetSelectedCheckBoxes(tvMain.Items, tvMain.ItemContainerGenerator);



            string str = "";
            foreach (CheckBox chk in list)
            {

                if (str.Length > 0)
                    str += ", ";

                //str += chk.Content;
                str += chk.Tag.ToString();
            }

            MessageBox.Show(str);
        }

        #endregion

        #region Public Methods

        private void FillTree()
        {
            foreach (WorldArea area in WorldArea.GetAll())
            {
                tvMain.Items.Add(area);
            }
        }

        private List<CheckBox> GetSelectedCheckBoxes(ItemCollection items, ItemContainerGenerator generator)
        {
            List<CheckBox> list = new List<CheckBox>();
            foreach (object area in items)
            {

                TreeViewItem item = (TreeViewItem)generator.ContainerFromItem(area);

                if (item != null)
                {
                    UIElement elemnt = Utility.GetChildControl(item, "chk");
                    if (elemnt != null)
                    {
                        CheckBox chk = (CheckBox)elemnt;
                        if (chk.IsChecked.HasValue && chk.IsChecked.Value)
                        {
                            list.Add(chk);
                        }
                    }


                    List<CheckBox> l = GetSelectedCheckBoxes(item.Items, item.ItemContainerGenerator);
                    list = list.Concat(l).ToList();
                }
            }

            return list;
        }

        #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