0

I have to bind Grid Drop Event and PreviewMouseLeftButtonDown event in ViewModel. I have a RelayCommand. But it is done only for passing the object, I have to pass the routed event by using the command and also for MouseButtonEventArgs. my sample code is as below, please give any suggestion for using the routed event args and MouseButtonEventArgs in viewmodel.

            <Grid
                x:Name="mainGrid"                    
                AllowDrop="True"
                Background="#F0F0F0">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Drop">
                        <cmd:EventCommandExecuter Command="{Binding GridDrop}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Grid>
<Grid Background="LightBlue" PreviewMouseLeftButtonDown="Grid_PreviewMouseLeftButtonDown">

EventCommandExecuter

public class EventCommandExecuter : TriggerAction<DependencyObject>
{
    #region Constructors

    public EventCommandExecuter()
        : this(CultureInfo.CurrentCulture)
    {
    }

    public EventCommandExecuter(CultureInfo culture)
    {
        Culture = culture;
    }

    #endregion

    #region Properties

    #region Command

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register("Command", typeof(ICommand), typeof(EventCommandExecuter), new PropertyMetadata(null));

    #endregion

    #region EventArgsConverterParameter

    public object EventArgsConverterParameter
    {
        get { return (object)GetValue(EventArgsConverterParameterProperty); }
        set { SetValue(EventArgsConverterParameterProperty, value); }
    }

    public static readonly DependencyProperty EventArgsConverterParameterProperty =
        DependencyProperty.Register("EventArgsConverterParameter", typeof(object), typeof(EventCommandExecuter), new PropertyMetadata(null));

    #endregion

    public IValueConverter EventArgsConverter { get; set; }

    public CultureInfo Culture { get; set; }

    #endregion

    protected override void Invoke(object parameter)
    {
        var cmd = Command;

        if (cmd != null)
        {
            var param = parameter;

            if (EventArgsConverter != null)
            {
                param = EventArgsConverter.Convert(parameter, typeof(object), EventArgsConverterParameter, CultureInfo.InvariantCulture);
            }

            if (cmd.CanExecute(param))
            {
                cmd.Execute(param);
            }
        }
    }
}

I want to pass object and RoutedEventArgs like below in viewmodel. Please help

public void Grid_Drop(object sender, RoutedEventArgs e)
{
}    
Achu_L
  • 175
  • 1
  • 13
  • Possible duplicate of [MVVM Passing EventArgs As Command Parameter](https://stackoverflow.com/questions/6205472/mvvm-passing-eventargs-as-command-parameter) – Mark Feldman Jul 17 '18 at 09:16
  • Voting to close this because it's a duplicate, short answer is you can also use a generic version of RelayCommand that accepts a specific type and bind to it with something like EventToCommand (assuming you're using MVVM Lite). And if you don't want to use the event parameter type itself you can also use EventArgsConverter with a converter to change it into whatever you need. – Mark Feldman Jul 17 '18 at 09:18
  • @MarkFeldman, I am not using MVVM light. I try your link – Achu_L Jul 17 '18 at 09:32
  • @MarkFeldman, In your link the accepted answer contains (cmd:EventToCommand) reference, But EventToCommand is not expained. I have searched many sites but EventToCommand is not there. Please help how to done EventToCommand. – Achu_L Jul 17 '18 at 09:55
  • EventToCommand is built into numerous MVVM libraries, including MVVM Lite. A simple Google will return the source code including [here](https://code.msdn.microsoft.com/VERY-Easy-MVVM-MVVMExtraLit-9a24e749/sourcecode?fileId=66304&pathId=1297845811). – Mark Feldman Jul 17 '18 at 10:24
  • @MarkFeldman, Please see my updated code. I have to pass object and RoutedEventArgs in viewmodel. But my code only pass the RountEventArgs. Please help – Achu_L Jul 17 '18 at 11:22

1 Answers1

0

I feel like commands are often overkill for such simple tasks. You can simply declare your ViewModel in the code behind of your view like so:

    public partial class MainWindow : Window
    {
        private ViewModel _vm;
        public ViewModel Vm
        {
            get { return _vm;}
            set
            {
                _vm = value ;                
            }
        }
        //....Constructor here.... 
      }

Then create a public event :

public event RoutedEventHandler OnGridDrop;

and call it in :

public void Grid_Drop(object sender, RoutedEventArgs e)
{
    OnGridDrop?.Invoke(sender,e)
}    

Now you only need to initialize your ViewModel:

public MainWindow()
{     
    InitializeComponent();
    Vm = new ViewModel();
    OnGridDrop += Vm.OnGridDrop;
}

and subscribe a corrsponding handler that you declared in your ViewModel.

oldsqlwnb
  • 21
  • 6