如何在 TreeView 中选择下一个/上一个 TreeViewItem?
我有第一个/最后一个/上一个/下一个按钮,可以更改 TreeViewItem 的选定子节点。 将第一个和最后一个节点设置为选定的不是问题。 例如,要选择最后一个子节点:
TreeViewItem selectedItem = (myTreeView.SelectedItem as TreeViewItem);
TreeViewItem ParentItem = (selectedItem.Parent as TreeViewItem);
(ParentItem.Items[ParentItem.Items.Count - 1] as TreeViewItem).IsSelected = true;
将上一个/下一个项目设置为被选择的最简单/最优雅的方法是什么?
谢谢!
I have First/Last/Previous/Next buttons that change the selected child node of a TreeViewItem. Setting the First and Last node as selected is not a problem. For example, to select the last child node:
TreeViewItem selectedItem = (myTreeView.SelectedItem as TreeViewItem);
TreeViewItem ParentItem = (selectedItem.Parent as TreeViewItem);
(ParentItem.Items[ParentItem.Items.Count - 1] as TreeViewItem).IsSelected = true;
What would be the easiest/most elegant way to set the Previous/Next item as being selected?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以用 IndexOf 调用替换 for 循环。
当然,最好检查一下 index + 1 是否是有效的集合索引。
对于前一个同级,它将是索引 - 1。
You could replace the for loop with a IndexOf call.
And of course it will be good to check if index + 1 is a valid collection index.
And for the previous sibling it will be index - 1.
我的变体至少适用于 4 级树。
如果光标遇到最后一个树视图项目,它将从开始处开始。
如果您在第一个节点按“上一个”,您将停留在原地。
My variant works for me at least for 4-level tree.
If cursor meet last treeviewitem, it goes at start.
If you press Previous at first node, you stay in place.
它并不优雅,但很有效。 有人能想出更好的解决方案吗? (显示“下一步”功能)
It's not elegant, but it works. can anyone come up with a better solution? ("Next" functionality shown)