title: WPF - How to get treeviewitem when user clicks on the checkbox
body: <p>I have defined the <code>ItemTemplate</code> for the <code>TreeView</code> in the code behind C#. In that <code>ItemTemplate</code>, I have a <code>CheckBox</code> and a label defined in it. The <code>Tag</code> and <code>Text</code> properties of the <code>CheckBox</code> and label is binded with the data.</p>
  
  <p>Now, I'd like to get the <code>TreeViewItem</code> object whenever the user clicks on the CheckBox. <strong>I used the click event on the <code>CheckBox</code> but that event didn't give me the <code>TreeViewItem</code>'s object</strong>. </p>
  
  <p>I would really appreciate if someone could explain the solution for it. </p>
  
  <p>Here's the code written in C#:</p>
  
  <pre><code>public static void FillTree()
  {
      //assign treeview itemtemplate
      BIMExplorerUserControl.Instance.BimTreeView.ItemTemplate = GetHeaderTemplate();
      BIMExplorerUserControl.Instance.BimTreeView.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);
  
      foreach (var category in ViewModel.Instance.DefaultExplorerView)
      {
          BIMExplorerUserControl.Instance.BimTreeView.Items.Add(category);
  
      }
  }
  
  
  
  public static DataTemplate GetHeaderTemplate()
  {
      //create the data template
      DataTemplate dataTemplate = new DataTemplate();
  
      //create stack pane;
      FrameworkElementFactory stackPanel = new FrameworkElementFactory(typeof(StackPanel));
      stackPanel.Name = "parentStackpanel";
      stackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
  
      // Create check box
      FrameworkElementFactory checkBox = new FrameworkElementFactory(typeof(CheckBox));
      checkBox.Name = "chk";
      checkBox.SetValue(CheckBox.NameProperty, "chk");
      checkBox.SetValue(CheckBox.TagProperty, new Binding());
      checkBox.SetValue(CheckBox.MarginProperty, new Thickness(2));
      checkBox.SetValue(CheckBox.TagProperty, new Binding() { Path = new PropertyPath("Name") });
      checkBox.AddHandler(CheckBox.ClickEvent, new RoutedEventHandler(CheckedEvent));
      stackPanel.AppendChild(checkBox);
  
  
      // create text
      FrameworkElementFactory label = new FrameworkElementFactory(typeof(TextBlock));
      label.SetBinding(TextBlock.TextProperty, new Binding() { Path = new PropertyPath("Name") });
      label.SetValue(TextBlock.ToolTipProperty, new Binding());
  
      stackPanel.AppendChild(label);
  
  
      //set the visual tree of the data template
      dataTemplate.VisualTree = stackPanel;
  
      return dataTemplate;
  
  }
  
  
  static void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
  {
      if (BIMExplorerUserControl.Instance.BimTreeView.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
      {
          foreach (var category in ViewModel.Instance.DefaultExplorerView)
          {
              TreeViewItem item = (TreeViewItem)BIMExplorerUserControl.Instance.BimTreeView.ItemContainerGenerator.ContainerFromItem(category);
              if (item == null) continue;
              item.IsExpanded = false;
              if (item.Items.Count == 0)
              {
                  foreach (var element in category.Elements)
                  {
                      item.Items.Add(element);
                  }
              }
          }
      }
  
  }
  
  private static void CheckedEvent(object sender, RoutedEventArgs e)
  {
      CheckBox checkbox = (CheckBox)e.Source;
      var name = checkbox.Tag.ToString();
  
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/ji7II.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ji7II.png" alt="enter image description here"></a></p>