WPF - 在 ListView 中使用 KeyDown 构建按键字符串
我有一个列表视图,其中有十几行绑定到 xml。我希望能够将光标定位到某个记录。例如: 我有一个具有这些 ID、名称、值的:
1, Johny, Cash, USA
2, Jean-Michel, Jarre, France
3, Jeanette, , USA
当我键入“Je”时,SelectedRow 将定位到 ID 2。当我键入“Jeane”时,SelectedRow 将定位到 ID 3。我只是想可以在列表视图中搜索并转到正确的记录。我开始构建 SearchString,此时我陷入了困境:
WPF 中唯一的可能性是使用 KeyDown 事件。不幸的是,这个事件返回一种 Key,我无法将其转换为字符串。例如,当我按“A”时,SearchString 将是“A”。当我继续输入“B”时,SearchString 将是“AB”等。当 SelectedItem 更改时,SearchString 将设置为 String.Empty。没有可用的 KeyCode 或其他有用的属性/方法。
揉头的动作来了。如何构建我需要的 SearchString? 当我尝试 e.Key.ToString() 时,我得到了非常有趣的字符串 - 例如,对于数字键盘上的 0,我得到一个键“Numpad0”,对于“,”我得到“OemComma”等。我还尝试了 TryParse 方法来字符,对于键“3”,我得到一个值“#”等,它仅适用于字母 A 到 Z,对于其他键 TryParse 返回 false。
解决这个问题的唯一方法是构建一个带有很长的“case e.Key.ToString() of”的转换表:
"A": SearchString = SearchString + "A";
"System", SearchString = SearchString + " ";
"Numpad0", SearchString = SearchString + "0";
"ArrowUp", do nothing
等等等等。
是否有一种更聪明和简单的方法来做到这一点??还是只见树木不见森林?
I have a listview with a dozen of rows binded to xml. I'd like to have a possibility to locate and position the cursor to a certain record. For example:
I have a with these ID, Name, Value:
1, Johny, Cash, USA
2, Jean-Michel, Jarre, France
3, Jeanette, , USA
When I would type "Je", the SelectedRow would be positioned to ID 2. When I would type "Jeane", the SelectedRow would be positioned to ID 3. Simply I'd like to have a possibility to search and go to the proper record in the listview. I started building the SearchString and at this point I got stuck:
The one and only possibility in WPF is to use the KeyDown event. Unfortunately, this event return a kind of Key, which I was not able to convert to a string. E.g. when I press "A", SearchString would be "A". When I continue typing "B", SearchString would be "AB" etc. When SelectedItem changes, SearchString will be set to String.Empty. No KeyCode or other useful property/method is available.
And here comes the head rubbing. How can I build the SearchString I need?
When I tried e.Key.ToString(), I got really funny strings - e.g. for 0 on Numpad I get a Key "Numpad0", for "," I get "OemComma" etc. I was trying also the TryParse method to char, for key "3" I get a value of "#" etc, it only works flawlessly just for letter A through Z, for other keys TryParse returns false.
The one and only way how to solve this is to build a translation table with a very long kind of "case e.Key.ToString() of":
"A": SearchString = SearchString + "A";
"System", SearchString = SearchString + " ";
"Numpad0", SearchString = SearchString + "0";
"ArrowUp", do nothing
etc etc etc.
Isn't there a more clever and simple way to do this?? Or I just don't see the trees because of the forest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
改为处理 PreviewTextInput。参考: http:// Social.msdn.microsoft.com/Forums/en-US/wpf/thread/3fcfbe53-2c72-4fec-a997-dc881d1de72a。
编辑:
注意:ListView(以及 ListBox)在内部处理某些键的 KeyDown 事件以执行选择和导航。下面是 ListView 内部处理的键:
Key.Space 特别令人感兴趣,因为当在 ListView 上按下空格时,PreviewTextInput 事件不会被触发。因此,要完成您的解决方案,您还必须为 ListView.PreviewKeyDown 事件添加一个处理程序,并检查是否按下了空格键,以便附加正确的空白文本。
Handle PreviewTextInput instead. Reference: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/3fcfbe53-2c72-4fec-a997-dc881d1de72a.
EDIT:
Note: The ListView (and ListBox also) internally handles the KeyDown event for some keys to perform selection and navigation. Below are the keys that are internally handled by the ListView:
Key.Space is of particular interest because when space is pressed on a ListView, the PreviewTextInput event will not get fired. So to complete your solution, you would have to also add a handler for the ListView.PreviewKeyDown event and check if the space key was pressed in order for you to append the correct whitespace text.