0
votes

I have a treeview panel. In the panel, there are several child nodes. Some of them are only a header.

The way I create the treeview:

treeviewpaneL.Items.Add(art);
art.Items.Add(prt);

some if statement....

TreeViewItem cldhdr = new TreeViewItem() { Header = "ChildNodes:" };
prt.Items.Add(cldhdr);
TreeViewItem cld = new TreeViewItem() .......
........
.....
cldhdr.Items.Add(cld);

Treeview:

Node1
  ChildNodes:           (This is header only.  It appears if child node exists)
   Childnode1
   Childnode2
   childnode3

Node2
Node3
  ChildNodes:           
   Childnode1
   Childnode2
   childnode3

Node4
Node5

In my treeview there are also images in front of all nodes. It's a code driven treeview. In the xaml part i have only:

<TreeView x:Name="treeviewpaneL" SelectedItemChanged="treeviewpaneL_SelectedItemChanged" >
                    </TreeView>

What I want to do is when I click on any of the treeview items, I need to get its index number.

My code is:

private void treeviewpaneL_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {

 int index = 0;
 ItemsControl parent = ItemsControl.ItemsControlFromItemContainer(prt);


foreach (var _item in parent.Items)
        {


            if (_item == treeviewpaneL.SelectedItem)
            {
                selectedNodeIndex = index;
                MessageBox.Show(selectedNodeIndex.ToString());
                break;
            }
            index++;
        }
 } 

With the code above, I can get the index of Node1,Node2,Node3, Node4 and Node5 as 0,1,2,3,4

What I want is to get the index numbers as:

Node1  = 0
Childnode1 = 1       (Skipping the header)
Childnode2 = 2
Childnode3 = 3
Node2  = 4
....
....
....

What am I missing?

2
From what i think every treeviewitem internally needs to have an index , your header "ChildNodes:" is still a treeviewitem to witch the treeview control will alocate an index. - Sebo Zoltan
if i can't find a way for skipping the header in order to get the indexes, i can get rid of the header. But still i need to loop through the child nodes. - ZMC
Here is the solution: Please make a custom MyTreeViewItem class which is derived from TreeViewItem class and make a property like "Index". So in treeviewpaneL_SelectedItemChanged event, cast the selected object to MyTreeViewItem and get your index - Ugur
Could you please explain breifly how to do the MyTreeViewItem class? - ZMC
@ZMC, please check the answer. - Ugur

2 Answers

0
votes

Here is the solution, first of all your "MyTreeViewItem"

public class MyTreeViewItem :TreeViewItem
{
    private int _index;
    public int Index
    {
        get { return _index; }
        set { _index = value; }
    }

    public MyTreeViewItem() : base() 
    {

    }
}

and usage;

            MyTreeViewItem art = new MyTreeViewItem();
            art.Header = "Node1";
            art.Index = 1; 


            MyTreeViewItem prt = new MyTreeViewItem();
            prt.Header = "Child1";
            prt.Index = 2;


            art.Items.Add(prt);


            treeviewpaneL.Items.Add(art); 

and event;

  private void treeviewpaneL_SelectedItemChanged(object sender,   RoutedPropertyChangedEventArgs<object> e)
        {
            MyTreeViewItem selectedItem = e.NewValue as MyTreeViewItem;
            if (selectedItem != null) 
            {
                MessageBox.Show("" + selectedItem.Index);

            }
        }
0
votes

To get the index of the currently selected item:

MyTreeView.Items.IndexOf(MyTreeView.SelectedItem);