I was able to get this to work with the xaml below.
There were two basic issues I ran into with your code:
- Both levels of your treeview were using "DataTemplate" instead of the "outer" one being defined as a "HierarchicalDataTemplate" -- so you had to put the ItemsSource on the TreeViewItem, which appears to have caused the "selection as a group" issue
I was having trouble getting selection to work at all using TreeViewItems, but after converting the DataTemplates to show a Label (you could display any sort of simple or complex set of controls in here), it worked perfectly. WPF automatically inserts "TreeViewItem" controls to wrap whatever you bind into a TreeView, so you don't need to do it explicitly.
<TreeView DockPanel.Dock="Left" Height="auto" Name="treeView1" Width="217" SelectedItemChanged="UIProfileTreeViewSelectedItemChanged" ItemsSource="{Binding}"> <TreeView.DataContext> <XmlDataProvider Source="Profiles.xml" XPath="/Profiles/Customer"/> </TreeView.DataContext> <TreeView.Resources> <HierarchicalDataTemplate DataType="Customer" ItemsSource="{Binding XPath=Profile}"> <Label Content="{Binding XPath=Name}" /> </HierarchicalDataTemplate> <DataTemplate DataType="Profile"> <Label Content="{Binding XPath=DisplayText}" /> </DataTemplate> </TreeView.Resources> </TreeView>
Hope this helps!
Guy Starbuck
2009-10-09 19:32:46