WPF & Listview. Shift - selecting multiple items. Wrong start item

心已入冬 提交于 2019-12-05 02:40:34

I've asked on MSDN about this issue. Surprisingly the cause of this problem is SelectionMode

The problem may be in the ListBox code (ListView derives from ListBox):

protected override void OnSelectionChanged(SelectionChangedEventArgs e)
{   ...
    if ((this.SelectionMode == SelectionMode.Single) && (base.SelectedItem != null))
    {
       ...
        if (selectedItem != null)
        {
            this.UpdateAnchorAndActionItem(selectedItem);
    }
}

The UpdateAnchorAndActionItem(selectedItem) is not called if the SelectionMode is Extended.

So, in the code behind you have to do next:

list.SelectionMode = SelectionMode.Single;
list.SelectedIndex = 4;
list.SelectionMode = SelectionMode.Extended;

Don't quite understand how be in case of MVVM.

Upd1

I have created custom ListView. It will do inside the mentioned above logic. In this case it must work as you expect even in MVVM. I hope it will help you.

public class MyListView:ListView
{
    protected override void OnSelectionChanged(SelectionChangedEventArgs e)
    {
        //if it is multiselection than execute standard logic
        if(SelectedItems.Count!=1)
        {
            base.OnSelectionChanged(e);
            return;
        }
        var mode = SelectionMode;
        SelectionMode = SelectionMode.Single;
        base.OnSelectionChanged(e);
        SelectionMode=mode;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!