0

I have a TreeView and there are multiple layers to the TreeView using HierarchicalDataTemplates. Each TreeViewItem currently displays the "name" of the object that it represents. I now need to add CheckBoxes to the children of the tree which I have figured out somewhat, but I haven't figured out to access the NAME of the checkbox when it is checked.

To be more specific, I need the name of the checkbox that is checked because I need to apply a filter to some data accordingly. How do I access the name of each CheckBox when it is checked if the CheckBox and Name of the object for the TreeViewItem are created dynamically from data?

Kevin Quiring
  • 639
  • 1
  • 11
  • 26

1 Answers1

1

i'm assuming you have a corresponding command in your viewmodel which all the checkbox items in the tree are bound to.

bind to a command and send to current name of the ComboBoxItem as a CommandParameter

  <DataTemplate x:Key="ListBoxItemTemplate" DataType="{x:Type ListBoxItem}">
        <CheckBox Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}, 
                  Path=DataContext.CheckBoxItemCheckedCommand}"
                  CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Name}"/>                       
   </DataTemplate>
eran otzap
  • 12,293
  • 20
  • 84
  • 139
  • Appreciate the response, not too familiar with commands. Can you give me an example of what the CheckBoxItemCheckedCommand would look like in the viewmodel and how to set that up vs. an event or something else. – Kevin Quiring Oct 04 '12 at 17:05
  • sure , i'll add it to my answer later on . – eran otzap Oct 04 '12 at 19:01