Multiselect on GridView in WPF ListView loses selection on mouse click

We overrode the ListViews View using a GridView so that we could display the data in the format our users required. The issue that we encountered was that after we introduced MultiSelect, our drag and drop didn’t work as expected.

You could select multiple rows, but when you then clicked on the selection area to “drag” them, then it would unselect everything but the one you specifically clicked on.

Not the greatest of behaviors.

So the trick is rather simple, yet strange. Override the ListViewItem class, and use it to override the generated items.

Your new ListView

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Windows;
using System.Collections.ObjectModel;
using System.Collections;
using System.Collections.Specialized;
using System.Windows.Controls.Primitives; 

namespace Seekford{
    public class MyListView: ListView    {
        protected override DependencyObject GetContainerForItemOverride()
        {            return new MyListItem();
       }
     protected override bool IsItemItsOwnContainerOverride(object item)
      { 
           return item is MyListItem;
       } 

     }
}

Your new ListViewItem

using System;
using System.Windows.Controls;
using System.Windows.Input;
using System.ComponentModel;

namespace Seekford{
      public class MyListItem : ListViewItem	{

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            if (IsSelected)
                return;
            base.OnMouseLeftButtonDown(e);
       }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            if(IsSelected)
                base.OnMouseLeftButtonDown(e);
        }
 }
}

Nice and easy! Kludgy, but seems to work.

Happy Coding!

0 Replies to “Multiselect on GridView in WPF ListView loses selection on mouse click”

  1. Nice work! I got stuck on this issue trying to implement a traditional drap/drop experience with a custom control and tried a bunch of stuff. This was the only solution that works exactly the way I need it to work without interfering with other listview operations. Thanks!

  2. I spoke too soon. This wasn’t working exactly right. You are unable to use the ctrl button to click on individual lines and un/select them.

    I was able to get this behavior to work by modifying the ListViewItem overrides to do different actions when Ctrl was used:

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
    if (IsSelected && ((Keyboard.Modifiers & ModifierKeys.Control) == 0))
    return;

    base.OnMouseLeftButtonDown(e);
    }

    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
    {
    if ((Keyboard.Modifiers & ModifierKeys.Control) == 0)
    base.OnMouseLeftButtonDown(e);

    base.OnMouseLeftButtonUp(e);
    }

  3. I have tried this as I had the exact same problem.

    However our list views contain 11,000 items. When changing from ListViewItem to CustomListViewItem, our load times went from seconds to minutes.

    For some reason virtualization is breaking, any ideas on how to fix this?

    1. I would make sure you aren’t doing any extra code in the initialization of the CustomListViewItem. Also, did you change the panel type for the control to something that isn’t supporting virtualization? You also have to set the virtualization property on the listview.

  4. Dan :
    I have tried this as I had the exact same problem.
    However our list views contain 11,000 items. When changing from ListViewItem to CustomListViewItem, our load times went from seconds to minutes.
    For some reason virtualization is breaking, any ideas on how to fix this?

    Try doing this :

Leave a Reply

Your email address will not be published. Required fields are marked *