Reputation: 16101
I have the following XAML:
<TreeView>
<TreeViewItem ItemsSource={Binding} Header="TopMost" IsExpanded="True">
<TreeViewItem.ItemTemplate>
<DataTemplate>
<TreeViewItem>
<TreeViewItem.Header>
<TextBlock Text="{Binding SubTopic}"/>
<TreeViewItem.Header>
<!-- further data representation -->
</TreeViewItem>
</DataTemplate>
</TreeViewItem.ItemTemplate>
</TreeViewItem>
<TreeViewItem>
</TreeViewItem>
</TreeView>
The first TreeViewItem does expand and show its children, but the selector that should be visible on the left of the text TopMost is not present. I can get the children of the main TreeViewItem to collaps and expand by double clicking the TopMost Header. How do I solve this?
Upvotes: 1
Views: 3037
Reputation: 62919
Your XAML works fine in a default WPF project except for a typo (I changed the second <TreeViewItem.Header> to </TreeViewItem.Header>). When I run it I see the selector (little triangle) beside the word "TopMost", and it works correctly.
I would:
Note
There's no law that says you can't use TreeViewItems inside DataTemplates like you are doing, but it usually is not a good idea because it defeats the purpose of the <TreeView>, which is to provide selection, focus, and accessibility support to multiple TreeViewItems.
You will find that:
This is because TreeView is not able to do its job: The DataTemplate is hiding the TreeViewItems from the TreeView so it only sees the top-level items. Check out HierarchicalDataTemplate for a good way to enable the TreeView object to see the whole tree.
Upvotes: 4