WPF ListView Keyboard Navigation Problem

徘徊边缘 提交于 2019-12-23 16:50:36

问题


I've a listview like this

ListView:
————-----

  • Mango
  • Orange
  • Grapes
  • Grapes
  • Grapes
  • Apple
  • Strawberry

Whenever i navigate using downarrow, the BlueHighlight pauses at the first Grapes, a dotted rectangle start from second grapes and pauses at the third grapes, then the BlueHighlight resumes from Apple. This seems weird and it grows more weird when the navigation is upwards. It jumps from Apple to Orange or mango.

Is this due to Virtualization?
It seems only the duplicate data (grapes) is creating the problem. Any Help?


回答1:


The dotted rectangle is your keyboard focus. The blue rectangle is your selection.

As you move down your keyboard focus tracks where you are. The selection, however, tracks which item is selected. When the same item is in the list several times, the selection rectangle can only be shown on one of them.

To make this work the way you are expecting, wrap your items inside your ObservableCollection. So instead of:

coll.Add(fruit);

you would write

coll.Add(new FruitWrapper(fruit));

In your ListBox your ItemTemplate can include a single ContentPresenter that presents the fruit inside the wrapper (eg. <ContentPresenter Content="{Binding Fruit}" />).




回答2:


Think of the blue highlight as the selected data item. Grapes is duplicated, so the data selection doesn't change.

The dotted rectangle is the keyboard focus, which only cares about the ListViewItem that represents the data item.

So there's one Grapes object represented by 3 ListViewItem objects.




回答3:


You have the same "Grapes" object in ObservableCollection 3 times, I mean the object with same reference. And Listbox is mess with this. Each element should be an uncial instance.



来源:https://stackoverflow.com/questions/2272103/wpf-listview-keyboard-navigation-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!