0

What I want: If I do a Left DoubleClick on a ListViewItem I want to invoke a Method.

What I have:

Xaml:

 <ListView ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
                      Name="StructListView"
                      Margin="15" 
                      Grid.Row="2" 
                      BorderThickness="0" 
                      SelectedItem="{Binding SelectedStructObject}" 
                      ItemsSource="{Binding StructObjects, Mode=TwoWay}"
                      HorizontalAlignment="Left"
                      HorizontalContentAlignment="Left">

                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseDoubleClick">
                        <i:InvokeCommandAction 
                    Command="{Binding Command}"
                    CommandParameter="{Binding ElementName=StructListView, Path=SelectedItem}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                ...

MainViewModel:

public class MainViewModel : BaseViewModel
{
    private StructElement _selectedStructObject;

    public StructElement SelectedStructObject
    {
        get { return _selectedStructObject; }
        set
        {
            if (_selectedStructObject != value)
            {
                _selectedStructObject = value;
                OnPropertyChanged();
            }
        }
    }

    public RelayCommand Command { get; set; }

    public ObservableCollection<StructElement> StructObjects { get; set; }

    private StructElement _structElement;

    public StructElement StructObject
    {
        get { return _structElement; }
        set
        {
            if (_structElement != value)
            {
                _structElement = value;
                OnPropertyChanged();
            }
        }
    }


    public MainViewModel()
    {

        Command = new RelayCommand(o => { //Method; });
      
        StructObjects = new ObservableCollection<StructElement>();         

    }
}

RelayCommand:

public class RelayCommand : ICommand
{
    private readonly Action<object> execute;
    private readonly Predicate<object> canExecute;

    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
        {
            throw new ArgumentNullException("execute");
        }

        this.execute = execute;
        this.canExecute = canExecute;
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public bool CanExecute(object parameter)
    {
        if (canExecute == null)
        {
            return true;
        }

        return canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        execute(parameter);
    }

}

I know that if I work with a Event I could use something like:

void OnMouseDoubleClick(Object sender, MouseButtonEventArgs e)
{
    if (e.ChangedButton == MouseButton.Left)
    {
    // Method
    }
}

But since I am working with a RelayCommand, I dont have a Event like MouseButtonEventArgs? I thought about "triggering" the OnMouseDoubleClick with the RelayCommand?

I am quite knew to Programming and WPF, so if you have further Information on Things I should read into to understand the solution, I would be thankful for everything.

Thanks.

ominaei
  • 1
  • 1
  • [does this answer your question](https://stackoverflow.com/questions/862570/how-can-i-use-the-relaycommand-in-wpf)? You already have an EventTrigger that invokes a command named `Command` on MouseDoubleClick and passes the ListView's SelectedItem as a parameter. You need to implement `Command` as an ICommand (probably change the name to something more apt) and have it create a RelayCommand in its getter like the example I linked. – Vultuxe Oct 04 '21 at 17:02

1 Answers1

0

I came across a different Solution from the Link.

In my XAML I use "EventToCommand" from MVVMLight:

                    <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseDoubleClick">
                        <cmd:EventToCommand Command="{Binding LightCommand}"
                                            PassEventArgsToCommand="True"/>                            
                    </i:EventTrigger>
                    </i:Interaction.Triggers>

I made my RelayCommand generic so I can pass an Event (I hope I got the Description right):

 public class RelayCommand : ICommand
{
    private readonly Action<object> execute;
    private readonly Predicate<object> canExecute;

    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
        {
            throw new ArgumentNullException("execute");
        }

        this.execute = execute;
        this.canExecute = canExecute;
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public bool CanExecute(object parameter)
    {
        if (canExecute == null)
        {
            return true;
        }

        return canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        execute(parameter);
    }

}

In my MainViewModel I declare a RelayCommand with a MouseButtonEventArgs and Pass a Method that checks If the MouseButton was a Leftclick and does something:

public RelayCommand<MouseButtonEventArgs> LightCommand { get; set; }

public void dosomething(MouseButtonEventArgs e)
    {
        if (e.ChangedButton == MouseButton.Left) 
        {
            MessageBox.Show(e.OriginalSource.GetType().ToString());
        }           
    }


public MainViewModel()
    {       
        LightCommand = new RelayCommand<MouseButtonEventArgs>(dosomething);
    }
Dharman
  • 30,962
  • 25
  • 85
  • 135
ominaei
  • 1
  • 1