1 Replies - 379 Views - Last Post: 26 September 2019 - 01:16 PM Rate Topic: -----

#1 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 312
  • Joined: 13-March 12

Handle PreviewRightMouseButtonDown WPF MVVM

Posted 26 September 2019 - 11:48 AM

My ListView is bound to a collection of ViewModels. I have a context Menu attached to each item that allows the user to right click and select Remove to remove the item from the list. But this also selects the item. Searching the internet I found a few references to this problem. It seems the thing to do is monitor the events with a custom event handler and then tell the program the event has been handled. This way the click wont make it to the selected item point. Sounds reasonable. The various examples shows EventTrigger with the RoutedEvent. But they require the library "http://schemas.microsoft.com/expression/2010/interactivity" which I can't seem to include in my XAML.

So I want to right click, expose the context menu and run that command. The only way to prevent the Selected Item is to capture the rightmousebuttondown and stop processing when the desired work is done.


                <ListView ItemsSource="{Binding Samples}"
                      SelectedValue="{Binding SampleSelected}">
                        <Style TargetType="{x:Type ListViewItem}">
                                <EventTrigger RoutedEvent="RightMouseButtonDown">
                                    <i:CallMethodAction="{Binding RightMouseButton}"/>
                        <ContextMenu StaysOpen="True" 
                            <MenuItem Header="Remove" 
                                      Command="{Binding RemoveSampleCommand}"/>


        public void RemoveSample()
            if (_selectedSample != null)
                string dispName = _selectedSample.DisplayName;
                Samples.Remove(Samples.First(i => i.DisplayName == dispName));
                if (Results.Any(x => x.DisplayName == dispName))
                    Results.Remove(Results.First(x => x.DisplayName == dispName));

        private RelayCommand removeSample;

        public ICommand RemoveSampleCommand
                if (removeSample == null)
                    removeSample = new RelayCommand(param => this.RemoveSample());
                return removeSample;

The above will not build because the reference to i is no good.

Thanks for taking a look.

This post has been edited by Skydiver: 26 September 2019 - 11:56 AM
Reason for edit:: Fixed code tags.

Is This A Good Question/Topic? 1
  • +

Replies To: Handle PreviewRightMouseButtonDown WPF MVVM

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7140
  • View blog
  • Posts: 24,250
  • Joined: 05-May 12

Re: Handle PreviewRightMouseButtonDown WPF MVVM

Posted 26 September 2019 - 01:16 PM

Post the exact errors/warnings you are getting.

I suspect that you are just getting a schema validation warning. It shouldn't stop building unless things have changed since 2012. There is a way to add XML schemas to Visual Studio, but I don't recall right now because I used to just ignore those warnings.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1