WPF树视图:如何像在资源管理器中一样实现键盘导航?
我是第一次使用 WPF 树视图,并对它不做的所有基本事情感到惊讶。其中之一是键盘导航,在任何独立的树视图中实现,例如在 Windows 资源管理器或 Regedit 中。
这应该是这样工作的:
如果树视图具有焦点并且我输入(字母/数字),则选择应移动到当前所选项目下方与字符串匹配的第一个可见(也称为展开)项目我打字并把它带入视野。如果在当前项目下方找不到匹配项,则应从顶部继续搜索。如果未找到匹配项,则不应更改所选项目。
只要我继续输入,搜索字符串就会不断增长,搜索也会变得更加精细。如果我停止输入一段时间(2-5 秒),搜索字符串就会被清空。
我准备从头开始“手动”编程,但由于这是非常基本的,我想肯定有人已经做到了这一点。
I am using the WPF treeview for the first time and am astonished of all the basic things it does not do. One of those is keyboard navigation, implemented in any self-respecting treeview, e.g. in Windows Explorer or Regedit.
This is how it should work:
If the treeview has the focus and I type (letters/numbers) the selection should move to the first visible (aka expanded) item below the currently selected item that matches the string I typed and bring that into view. If not match is found below the current item the search should continue from the top. If no match is found, the selected item should not change.
As long as I continue typing, the search string grows and the search is refined. If I stop typing for a certain time (2-5 seconds), the search string is emptied.
I am prepared to program this "by hand" from scratch, but since this is so very basic I thought surely someone has already done exactly this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有趣的是,这似乎不是一个热门话题。不管怎样,与此同时,我已经开发了一个令我满意的问题解决方案:
我将一个行为附加到 TreeViewItems。在这种行为中,我处理 KeyUp 事件。在 KeyUp 事件处理程序中,我在可视化树显示时从上到下搜索它。如果我找到第一个匹配的节点(其名称以按下的键上的字母开头),我会选择该节点。
Funny, this does not seem to be a popular topic. Anyway, in the meantime I have developed a solution to the problem that satisfies me:
I attach a behavior to the TreeViewItems. In that behavior, I handle KeyUp events. In the KeyUp event handler, I search the visual tree top to bottom as it is displayed. If I find a first matching node (whose name starts with the letter on the key pressed) I select that node.
我知道这是一个老话题,但我想它对某些人来说仍然相关。我做了这个解决方案。它附加到 WPF TreeView 上的 KeyUp 和 TextInput 事件。除了 KeyUp 之外,我还使用 TextInput,因为我很难使用 KeyEventArgs 将“国家”字符转换为真实字符。使用 TextInput 就更顺利了。
I know that is an old topic, but I guess it is still relevant for some people. I made this solution. It is attached to the KeyUp and the TextInput event on a WPF TreeView. I'm using TextInput in addition to KeyUp as I had difficulty translating "national" chars to real chars with KeyEventArgs. That went much more smooth with TextInput.
我也在寻找键盘导航,令人惊讶的是,模板化项目的解决方案并不明显。
在 ListView 或 TreeView 中设置 SelectedValuePath 会产生此行为。
如果项目是模板化的,那么将附加属性:TextSearch.TextPath 设置为要搜索的属性的路径也可以解决问题。
希望这有帮助,它绝对对我有用。
I was also looking for keyboard navigation, amazing how not obvious the solution was for templated items.
Setting SelectedValuePath in ListView or TreeView gives this behavior.
If the items are templated then setting the attached property: TextSearch.TextPath to the path of the property to search on will also do the trick.
Hope this helps, it definitely worked for me.
由于这个问题在搜索时出现得最多,所以我想发布一个答案。
当我使用带有 HierarchicalDataTemplate 的数据绑定 TreeView 时,lars 的上述帖子对我不起作用,因为 Items 集合返回实际的数据绑定项,而不是 TreeViewItem。
我最终通过对各个数据项使用 ItemContainerGenerator 并使用 VisualTreeHelper 搜索“向上”以查找父节点(如果有)来解决此问题。我将其实现为静态帮助器类,以便我可以轻松地重用它(对我来说基本上是每个 TreeView)。
这是我的帮助器类:
我还保存了最后选择的节点,如这篇文章中所述:
这是上面的 TextInput,修改后使用此类:
请注意,此解决方案与上面的解决方案略有不同,因为我只搜索所选节点的子节点以及与所选节点处于同一级别的节点。
Since this question comes up most prominently when searching, I wanted to post an answer to it.
The above post by lars doesn't work for me when I'm using a databound TreeView with a HierarchicalDataTemplate, because the Items collection returns the actual databound items, not the TreeViewItem.
I ended up solving this by using the ItemContainerGenerator for individual data items, and the VisualTreeHelper to search "up" to find the parent node (if any). I implemented this as a static helper class so that I can easily reuse it (which for me is basically every TreeView).
Here's my helper class:
I also save the last selected node, as described in this post:
And here's the above TextInput, modified to use this class:
Note that this solution is a little bit different from the above, in that I only search the children of the selected node, and the nodes at the same level as the selected node.
它并不像我们期望的那么简单。但我找到的最好的解决方案在这里:
http://www.codeproject.com /Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode
如果您需要更多详细信息,请告诉我。
It is not very straightforward as we expect it to be. But the best solution I have found is here:
http://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode
Let me know if you need more details.