10

Problem explained:

Given a list containing 10 items.

  1. My first action is to (mouse) click on the second item.
  2. Secondly I have a button which is supposed to programmatically select an item.

For example:

listView.SelectedIndex = 4; 
//or
listView.SelectedItems.Add(listView.Items[4]);

The item is correctly selected.

  1. If I now press SHIFT and select the LAST item the selection starts at the clicked item and not the programmatically selected item.

One solution was to simulate a mouse click event, which worked but had side effects. Its also way to hacky.

It seems that the mouse event stores the starting item.

Is there something I have overlooked?

Artiom
  • 7,694
  • 3
  • 38
  • 45
TacticalTree
  • 103
  • 1
  • 6
  • It is something strange. It looks like this is WPF`s bug or feature. I think that the best you can do is to simulate this functionality "by hands" – dvvrd Aug 14 '12 at 11:17
  • When I click the button responsible for highlighting the button I also handle a mouse up event. However when also simulating a listview mouseclick, I never receive the mouse up event and this has consequences for the rest of the application. This makes sense since there are two mouse down clicks, one real and one simulated. – TacticalTree Aug 14 '12 at 12:05
  • No, try to implement your SHIFT pressed click functionality. Just handle clicks, check if sift pressed and add all the indexes between lastly selected – dvvrd Aug 14 '12 at 12:24

1 Answers1

8

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;
    }
}
Artiom
  • 7,694
  • 3
  • 38
  • 45
  • This solution worked like a charm. Did MSDN give any explanation regarding this issue? Is it a bug or a misunderstood feature? – TacticalTree Aug 15 '12 at 06:44
  • 1
    @TacticalTree For now, nothing concrete: *The UpdateAnchorAndActionItem(selectedItem) is not called if the SelectionMode is Extended. I am not sure, what this method actually does, but it sounds like it sets the item, which represents the bebinning of the extended selection.*. Here is the thread: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/2afac778-e17a-4f7f-a715-6cf37f1c53b1 – Artiom Aug 15 '12 at 06:54
  • 1
    http://social.msdn.microsoft.com/Forums/en/wpf/thread/705abd92-283c-403b-ad45-81cfa57a25e4 solved using reflection, I don't like this, but may be it will be interesting for smb. – Artiom Aug 16 '12 at 14:01