Wpf ListBox 使用键盘滚动问题

发布于 2024-10-21 00:50:36 字数 510 浏览 1 评论 0原文

好的,我有一个列表框,并为列表框项定义了一个触发器,如下所示:

 <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2024-10-28 00:50:36

哇,这是一个最有趣的触发器:)

这种行为是预期的,因为所选项目由 ListBoxItem 聚焦,并且当 ScrollBar 更新时,鼠标下方的新项目的 IsMouseOver 属性会发生变化。

如果同时进行键盘选择,您基本上需要禁用选择鼠标下的项目:

黑客警告:

EventManager.RegisterClassHandler(typeof(ListBoxItem), ListBoxItem.SelectedEvent, new RoutedEventHandler(
            (s, e) => e.Handled = (Keyboard.IsKeyDown(Key.Down) || Keyboard.IsKeyDown(Key.Up)) && (s as FrameworkElement).IsMouseOver), true);

将以上内容放在任何静态构造函数中。它很粗糙,但对我有用。

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:

EventManager.RegisterClassHandler(typeof(ListBoxItem), ListBoxItem.SelectedEvent, new RoutedEventHandler(
            (s, e) => e.Handled = (Keyboard.IsKeyDown(Key.Down) || Keyboard.IsKeyDown(Key.Up)) && (s as FrameworkElement).IsMouseOver), true);

Place the above in any static constructor. It is crude but works for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文