Wpf ListBox 使用键盘滚动问题
好的,我有一个列表框,并为列表框项定义了一个触发器,如下所示:
<DataTrigger Binding="{Binding IsMouseOver,RelativeSource={RelativeSource Self}}" Value="True">
<Setter Property="IsSelected" Value="True" />
</DataTrigger>
当我将鼠标移到列表框项上时,它就会被选中,这样就可以正常工作。当我尝试使用键盘向下箭头滚动列表时,问题就出现了。 因此,假设我的鼠标位于列表中的第三项上,并且默认情况下已选择该项。现在,当我按向下箭头时,第一个项目被选中!另外,当我按住向下箭头时,一旦选择移动到之前不在视图中的项目,只有可查看列表会正确滚动,选择将放回到鼠标悬停的项目上,并且滚动从那里!...换句话说,当鼠标根本不在列表框上时,键盘滚动工作正常...我真的希望我说清楚了。
我可能在这里做了一些愚蠢的事情,欢迎任何建议 谢谢
Ok, I have a listbox and have a trigger defined for a listboxitem as follows:
<DataTrigger Binding="{Binding IsMouseOver,RelativeSource={RelativeSource Self}}" Value="True">
<Setter Property="IsSelected" Value="True" />
</DataTrigger>
This works fine as soon as i move my mouse over a listboxitem it gets selected. the problem comes when i try scrolling the list with keyboard down arrow.
So assume that my mouse is over the third item in the list and thats selected by default. Now, when I press the down arrow, the first item is selected! Also, when i keeep the down arrow pressed, only the viewable list scrolls properly as soon as the selection moves to an item that was not in view before, the selection is put back on the item on which the mouse was over and scrolling starts from there!...so in other words, the keyboard scrolling works fine when the mouse IS NOT OVER THE LISTBOX at all....I really hope I made myself clear.
I may be doing something silly here and any suggestions are welcome
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哇,这是一个最有趣的触发器:)
这种行为是预期的,因为所选项目由 ListBoxItem 聚焦,并且当 ScrollBar 更新时,鼠标下方的新项目的 IsMouseOver 属性会发生变化。
如果同时进行键盘选择,您基本上需要禁用选择鼠标下的项目:
黑客警告:
将以上内容放在任何静态构造函数中。它很粗糙,但对我有用。
Wow, this is a most amusing trigger :)
This behavior is kind of expected since the selected item is focused by the ListBoxItem and when the ScrollBar updates the IsMouseOver property changes for the new item below the mouse.
You basically need to disable selecting the item under the mouse if there is keyboard selection going on at the same time:
Hack Warning:
Place the above in any static constructor. It is crude but works for me.