title: WPF TreeView with CheckBox - How to Get the list of Checked?
body: <p>I have implemented TreeView with Checkboxes from <a href="http://www.thebestcsharpprogrammerintheworld.com/blogs/treeview-with-checkbox-in-wpf-using-csharp.aspx" rel="nofollow">here</a>
  I shows perfectly well. Now I am wondering to get list of selected/checked items from the tree. I believe I need to code a recursive method. But the author has set the recursive method <code>GetTree()</code> to be Static and I can't get any clue to start the recursion. 
  Similar article is also found <a href="http://www.codeproject.com/Articles/28306/Working-with-Checkboxes-in-the-WPF-TreeView?fid=1524861&amp;df=90&amp;mpp=25&amp;noise=3&amp;prof=False&amp;sort=Position&amp;view=Normal&amp;spc=Relaxed&amp;fr=51#xx0xx" rel="nofollow">here</a>, I found in a way for my problem overe there also, but couldn't get any thing helpful. </p>
  
  <p>I am attaching whole code here for your reference :</p>
  
  <pre><code>    public class TreeViewModel : INotifyPropertyChanged
  {
      public TreeViewModel(string name)
      {
          Name = name;
          Children = new List&lt;TreeViewModel&gt;();
      }
  
      #region Properties
  
      public string Name { get; private set; }
      public List&lt;TreeViewModel&gt; Children { get; private set; }
      public bool IsInitiallySelected { get; private set; }
  
      bool? _isChecked = false;
      TreeViewModel _parent;
  
      #region IsChecked
  
      public bool? IsChecked
      {
          get { return _isChecked; }
          set { SetIsChecked(value, true, true); }
      }
  
      void SetIsChecked(bool? value, bool updateChildren, bool updateParent)
      {
          if (value == _isChecked) return;
  
          _isChecked = value;
  
          if (updateChildren &amp;&amp; _isChecked.HasValue) Children.ForEach(c =&gt; c.SetIsChecked(_isChecked, true, false));
  
          if (updateParent &amp;&amp; _parent != null) _parent.VerifyCheckedState();
  
          NotifyPropertyChanged("IsChecked");
      }
  
      void VerifyCheckedState()
      {
          bool? state = null;
  
          for (int i = 0; i &lt; Children.Count; ++i)
          {
              bool? current = Children[i].IsChecked;
              if (i == 0)
              {
                  state = current;
              }
              else if (state != current)
              {
                  state = null;
                  break;
              }
          }
  
          SetIsChecked(state, false, true);
      }
  
      #endregion
  
      #endregion
  
      void Initialize()
      {
          foreach (TreeViewModel child in Children)
          {
              child._parent = this;
              child.Initialize();
          }
      }
  
      public static List&lt;TreeViewModel&gt; setTree(TreeViewModel tree)
      {
          List&lt;TreeViewModel&gt; treeView = new List&lt;TreeViewModel&gt;();
          treeView.Add(tree);
          tree.Initialize();
  
          return treeView;
      }
  
      public static List&lt;TreeViewModel&gt; SetTree(string topLevelName)
      {
          List&lt;TreeViewModel&gt; treeView = new List&lt;TreeViewModel&gt;();
          TreeViewModel tv = new TreeViewModel(topLevelName);
  
          treeView.Add(tv);
  
          //Perform recursive method to build treeview 
  
          #region Test Data
          //Doing this below for this example, you should do it dynamically 
          //***************************************************
          TreeViewModel tvChild4 = new TreeViewModel("Child4");
  
          tv.Children.Add(new TreeViewModel("Child1"));
          tv.Children.Add(new TreeViewModel("Child2"));
          tv.Children.Add(new TreeViewModel("Child3"));
          tv.Children.Add(tvChild4);
          tv.Children.Add(new TreeViewModel("Child5"));
  
          TreeViewModel grtGrdChild2 = (new TreeViewModel("GrandChild4-2"));
  
          tvChild4.Children.Add(new TreeViewModel("GrandChild4-1"));
          tvChild4.Children.Add(grtGrdChild2);
          tvChild4.Children.Add(new TreeViewModel("GrandChild4-3"));
  
          grtGrdChild2.Children.Add(new TreeViewModel("GreatGrandChild4-2-1"));
          //***************************************************
          #endregion
  
          tv.Initialize();
  
          return treeView;
      }
  
      public static List&lt;string&gt; GetTree()
      {
          List&lt;string&gt; selected = new List&lt;string&gt;();
  
          //select = recursive method to check each tree view item for selection (if required)
  
          return selected;
  
          //***********************************************************
          //From your window capture selected your treeview control like:   TreeViewModel root = (TreeViewModel)TreeViewControl.Items[0];
          //                                                                List&lt;string&gt; selected = new List&lt;string&gt;(TreeViewModel.GetTree());
          //***********************************************************
      }
  
      #region INotifyPropertyChanged Members
  
      void NotifyPropertyChanged(string info)
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(info));
          }
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      #endregion
  }
  </code></pre>
  
  <p>I couldn't find appropriate to se the tree dynamically from my code using <code>SetTree(string topLevelName)</code> method, hence I added <code>setTree(TreeViewModel tree)</code> method to suit my needs. </p>
  
  <p>Can anyone please help me to implement GetTree() method to get list of items that is checked. If an item of the tree is selected, the TreeViewModel's IsChecked property is set to true. So IsChecked can be used to find if the item is selected r not.</p>