0

In my TreeView I've several values, in particular, this is the structure:

Nations -> Championships (child of each nation) -> Teams (childs of each championship).

For Nation that's a root node I execute a method that perform particular task, the same for championship, but, how can I check if the user has select the Teams? So the child of a Championship?

Actually I do this:

TreeViewItem rootNode = (TreeViewItem)e.NewValue;
if (rootNode.Parent is TreeView)
{
     soccerSeason.getChampionshipsForTeams(e);
}
else
{
     teams.getTeam(e);
}

This working only for Nation and Championship, I want execute another method for each team selected, how I can do this? Because Actually if I select a child of Championship, was run the getTeam method.

Bender
  • 523
  • 6
  • 21
  • In your else, before calling your processing function you can check the depth: rootNode.SelectedNode.Level -- however, do you have some other code that is traversing your TreeView? What you've shown isn't traversing nodes. – mjw Aug 28 '15 at 13:06
  • Could you show me an example? – Bender Aug 28 '15 at 13:20
  • Here is a sample of how to traverse nodes in a TreeView: http://stackoverflow.com/questions/19691286/how-to-iterate-through-all-nodes-of-a-treeview-control-c-sharp -- this is typically done with recursion, and once you've managed to collect all nodes in your tree, you can apply my first suggestion above to check the depth (node.Level) to determine the processing function to use (Nations= depth 1, Championships=depth 2, Teams=depth 3) – mjw Aug 28 '15 at 13:32

1 Answers1

0

I think you can make something like this for team:

TreeViewItem rootNode = (TreeViewItem)e.NewValue;
if (rootNode.Parent is TreeViewItem
           && (rootNode.Parent as TreeViewItem).Parent is TreeView)
{
                //it's team do something
}
Andrey
  • 160
  • 1
  • 9
  • Nope, this is for get the Championship not the Teams – Bender Aug 28 '15 at 13:20
  • var parent = rootNode.Parent; object pparent; if (parent is TreeViewItem && (pparent = (parent as TreeViewItem).Parent) is TreeViewItem && ( pparent as TreeViewItem).Parent is TreeView) { //it's team do something } – Andrey Aug 28 '15 at 13:23