0
votes

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?

1

1 Answers

1
votes

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>