title: Access Visual child Elements from viewmodel wpf
body: <p>I have a view as follows.. I want to access all the check boxes in the listview of the Main window in the viewmodel..But in the context of the main window it works fine..But in the view model if I say <code>IEnumerable&lt;CheckBox&gt; all = window.allboxes();</code>its not working..Please help..</p>
  
  <pre><code>public partial class MainWindow : Window
  {
  
  
      public MainWindow()
      {
          InitializeComponent();
  
          EntityViewModel ev = new EntityViewModel();
          this.DataContext = ev;
  
  
      }
  
      public IEnumerable&lt;CheckBox&gt; allboxes()
      {
          IEnumerable&lt;CheckBox&gt; myboxes = FindVisualChildren&lt;CheckBox&gt;(this);
          return myboxes;
      }
  
  
      public static IEnumerable&lt;T&gt; FindVisualChildren&lt;T&gt;(DependencyObject depObj) where T : DependencyObject
      {
          if (depObj != null)
          {
              for (int i = 0; i &lt; VisualTreeHelper.GetChildrenCount(depObj); i++)
              {
                  DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                  if (child != null &amp;&amp; child is T)
                  {
                      yield return (T)child;
                  }
  
                  foreach (T childOfChild in FindVisualChildren&lt;T&gt;(child))
                  {
                      yield return childOfChild;
                  }
              }
          }
  
      }
  }
  </code></pre>