2

I have a ListView of items that I'd like to right-click (offering options like delete, rename, etc.) The issue is when right-clicking an item it selects that item. I'd like to avoid that functionality while keeping the context menu available. If multiple items are selected, they should stay selected so the user can delete them via the context menu.

One solution proposed here was to just disable the right mouse button down event on the individual items, but I need that context menu because it's different from the one on the ListView itself. Therefore I seem to be out of ideas.

Edit: I would rather not have to clear items then reset the selection every time because this ListView can contain over 100 items. It is virtualized but still, that was the FIRST thing I thought of and it's my last resort. Keeping a list on the side that I have to constantly sync the ListView with will be annoying and results in lazy code.

I need to be able to multi-select ListView items then right-click and execute a command that will take into account the entire selection.

AliEgseem
  • 321
  • 4
  • 18
  • 1
    please have a look at the following question: "http://stackoverflow.com/questions/1075170/wpf-listview-right-click-problem" .. that should solve your problem – Sean Jun 17 '16 at 21:56
  • @Sean I gave up on the problem and have come back to it, it's still going on. That question isn't of much help. I need the context menu to show up for every item, I can't set e.Handled = true, that will suffocate any of the item's context menus from coming up. In that case, it's useful because the person only needs the ListView's context menu, not each item's. I need to be able to multi-select ListView items then right-click and execute a command that will take into account the entire selection. – AliEgseem Jul 18 '16 at 21:26

1 Answers1

0

I ran into this problem a few time with listboxes as well. I ended up making a custom control that skips mouse-down behavior completely by marking it as e.Handled=true and does the mouse-down behavior on mouseup.

    protected override void OnMouseUp(MouseButtonEventArgs e)
    {
        if (e.ChangedButton == MouseButton.Left)
        {
            var obj = ContainerFromElement((Visual) e.OriginalSource);
            if (obj == null)
                return;`

            var fe = obj as FrameworkElement;
            var item = fe as ListBoxItem;

            if (item != null)
            {
                SelectedItem = item.DataContext;
            }
        }
    }

    protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
    {
        if(e.LeftButton != MouseButtonState.Pressed)
            e.Handled = true;
    }
AliEgseem
  • 321
  • 4
  • 18
  • What is 'ContainerFromControl'? – Krythic Jan 17 '20 at 05:05
  • ItemsControl.ContainerFromElement , the above example is if the control is subclassed. This link shows if handled outside the control https://stackoverflow.com/a/6939090/4146066 – Hans Feb 25 '21 at 09:47