I am assuming what you what is this:
On click of the top level nodes (node0, node1, node2), I want to get all the children data nodes in the tree.
If this is what you need, here's the code:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TreeView x:Name="myTreeView" Height="271" HorizontalAlignment="Left" Margin="47,12,0,0" VerticalAlignment="Top" Width="236" SelectedItemChanged="myTreeView_SelectedItemChanged">
<TreeViewItem Header="node0" >
<TreeViewItem Header="data01" />
<TreeViewItem Header="data02" >
<TreeViewItem Header="data021" />
<TreeViewItem Header="data022" />
</TreeViewItem>
<TreeViewItem Header="data03" />
</TreeViewItem>
<TreeViewItem Header="node1" >
<TreeViewItem Header="data11" />
</TreeViewItem>
<TreeViewItem Header="node2" >
<TreeViewItem Header="data21" />
</TreeViewItem>
</TreeView>
</Grid>
</Window>
And the cs code behind to handle SelectedItemChanged event will look like this
private void myTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
foreach (TreeViewItem dataNode in ((TreeViewItem)e.NewValue).Items)
{
TraverseChildrenData(dataNode);
}
}
public void TraverseChildrenData(TreeViewItem treeViewItem)
{
//do whatever you want to do with child data item here..
MessageBox.Show(treeViewItem.Header.ToString());
foreach (TreeViewItem child in treeViewItem.Items)
{
TraverseChildrenData(child);
}
}