ListBox中按住Shift键并点击多选锚没有被正确设置 [英] ListBox Shift-Click Multi-Select Anchor is not being set properly

查看:406
本文介绍了ListBox中按住Shift键并点击多选锚没有被正确设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我束手无策试图弄清楚这一点。我经历了WPF,但我从来没有见过这样的事。



我有一个包含可选ListBoxItems一个列表框。列表中的项目可以通过鼠标点击或通过使用向上/向下箭头来选择。我使用SelectionMode.Extended,所以我的列表支持多选



问题:单击某个项目在列表中,然后按住Shift键单击另一个项目选择像你这样的项目正确范围内所期望的。不幸的是,使用上/下箭头不能正常工作 - 相反,这总是选择的范围似乎是基于了最后单击项目,不是使用箭头键选择的项目



例如:

  [项目1];  - 单击此项
[项目2< - 使用向下箭头,选择这一项
[项目3]< - 按住Shift键并单击此项

您会期望项目2和3将被选择,相反,所有的项目(1,2,和3)选择



<的对>所有列表框的属性有正确的值(即使用箭头键会更新selectedItems属性),我唯一的问题似乎是如何列表框在内部处理按住Shift键单击选择。据我所知,我相信这与ListBox的多选锚获得通过点击鼠标,而不是使用箭头键设置的事情。



有没有人遇到和解决过这个?有没有办法手动设置多选锚?感谢您的帮助!



克里斯


解决方案

我已经想通出分辨率这样的:



为了解决这个问题,必须改变的SelectedIndex后,将焦点设置到该项目:

 如果(的SelectedIndex大于0){
SelectedIndex--;
ListBoxItem的项目= ItemContainerGenerator.ContainerFromIndex(的SelectedIndex)为ListBoxItem的;
item.Focus();
}

这是你如何在列表中向下移动的选择。但是,您需要将焦点设置为按住Shift键单击选择来识别该项目为定位的项目。


I’m at my wit’s end trying to figure this out. I'm experienced in WPF, but I've never seen anything like this.

I have a ListBox that contains selectable ListBoxItems. Items in the list can be selected by mouse click or by using the up/down arrows. I’m using SelectionMode.Extended, so my list supports multiple selections.

The problem: Clicking on an item in the list, then Shift-Clicking on another item selects the correct range of items like you would expect. Unfortunately, using the up/down arrows does not work properly – instead, the range that’s selected always seems to be based off of the last CLICKED item, not the item that was selected with the arrow keys.

For example:

[Item 1] <- Click this item
[Item 2] <- Use the down arrow to select this item
[Item 3] <- Shift-Click this item

You would expect items 2 and 3 to be selected, instead, all of the items (1, 2, and 3) are selected.

All of the ListBox properties have the correct values (i.e. using the arrow keys updates the SelectedItems property), my only problem seems to be with how the ListBox is handling Shift-Click selection internally. To the best of my knowledge, I believe this has to do with the ListBox’s "multi-select anchor" getting set by mouse-click but not with the arrow keys.

Has anyone encountered and solved this before? Is there a way to set the "multi-select anchor" manually? Thanks for your help!

Chris

解决方案

I've figured out the resolution to this:

In order to solve this, you must set Focus to the item after changing the SelectedIndex:

if (SelectedIndex > 0) {
     SelectedIndex--;
     ListBoxItem item = ItemContainerGenerator.ContainerFromIndex(SelectedIndex) as ListBoxItem;
     item.Focus();
}

This is how you move the selection down in the list. However, you need to set focus to the item for the shift-click selection to recognize the item as the anchor.

这篇关于ListBox中按住Shift键并点击多选锚没有被正确设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆