开发者

Getting the SelectedIndex of a child node in a TreeView

开发者 https://www.devze.com 2023-03-13 18:18 出处:网络
I am currently working on a wpf project in C#. I have a treeview created that has parent nodes with childen nodes inside of it.

I am currently working on a wpf project in C#.

I have a treeview created that has parent nodes with childen nodes inside of it.

I was wondering if there was a way to get the index of the child node the user clicked on. (Simmilar to ".SelectedIndex" when using comboboxes)

I have tried Various ways such as:

int val =TreeView.SelectedItemProperty.GlobalInde开发者_JAVA百科x;

and

fileInput.IndexOf(treeView1.SelectedItem);

But they dont seem to work.

Any suggestions or comments are greatly appreciated.

Thanks


may you have to loop over tree nodes to get the index of SelectedItem. you can do that using OnItemSelected event.for ex.

Int32 selectedNodeIndex=-1;
private void TreeView1_OnItemSelected(Object sender,RoutedEventArgs e)
{
      Int32 index=0;
      foreach(var _item in TreeView1.Items)
      {
         if(_item==TreeView1.SelectedItem)
         {
                selectedNodeIndex = index;
                break;
         }
          index++;
      }    
}


This post discusses exactly what you need I think. About handling the SelectedNodeChanged event and also a custom piece of code for an event that fires when the currently selected node is clicked...because then the SelectedNodeChanged doesn't fire (the selected node doesn't change actually). Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号