获取 TreeView 中子节点的 SelectedIndex
我目前正在使用 C# 开发一个 wpf 项目。
我创建了一个树视图,其中包含父节点和子节点。
我想知道是否有办法获取用户单击的子节点的索引。 (与使用组合框时的“.SelectedIndex”类似)
我尝试过各种方法,例如:
int val =TreeView.SelectedItemProperty.GlobalIndex;
和
fileInput.IndexOf(treeView1.SelectedItem);
但它们似乎不起作用。
如有任何建议或意见,我们将不胜感激。
谢谢
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.GlobalIndex;
and
fileInput.IndexOf(treeView1.SelectedItem);
But they dont seem to work.
Any suggestions or comments are greatly appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能您必须循环遍历树节点才能获取
SelectedItem
的索引。您可以使用OnItemSelected
事件来做到这一点。may you have to loop over tree nodes to get the index of
SelectedItem
. you can do that usingOnItemSelected
event.for ex.这篇文章准确讨论了我认为您需要的内容。关于处理
SelectedNodeChanged
事件以及单击当前选定节点时触发的事件的自定义代码...因为SelectedNodeChanged
不会触发(所选节点实际上并没有改变)。祝你好运!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 theSelectedNodeChanged
doesn't fire (the selected node doesn't change actually). Good luck!