I use HierarchicalDataTemplate to bind my classes to TreeView using checkboxes. My code works fine, and everything displays fine, but I would like to be able to get a list of children of the element in my tree structure.
When I click on the checkbox, I want to be able to select the parent nodes and child nodes. If I had access to the TreeViewItem that was supposed to wrap this flag, I could easily do this, but the Parent property of the flag is null ... I can only access my classes that are displayed in the HierarchicalDataTemplate.
<TreeView Margin="12" Name="trv1" SelectedItemChanged="trv1_SelectedItemChanged">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type src:Location}" ItemsSource="{Binding Path=Sublocations}">
<CheckBox Content="{Binding Name}" Tag="{Binding}" IsChecked="{Binding IsChecked}" Click="checkBox_Click"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type src:Sublocation}" ItemsSource="{Binding Path=Children}">
<CheckBox Content="{Binding Name}" Tag="{Binding}" IsChecked="{Binding IsChecked}" Click="checkBox_Click"/>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type src:Child}">
<CheckBox Content="{Binding Name}" Tag="{Binding}" IsChecked="{Binding IsChecked}" Click="checkBox_Click"/>
</DataTemplate>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsSelected" Value="{Binding IsChecked}"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
source
share