-1

In my WPF MVVM (NET 3.5) app I have below button:

<Button AllowDrop="True"  
        Drop="btnDelete_Drop" 
        DragEnter="btnDelete_DragEnter"
        Content="Delete"/>

The button behavior is the following:

  • If some listview items are drag and drop on this button, then some event in the view are raised btnDelete_DragEnter" and "btnDelete_Drop". On drop, the dropped items are removed by calling a method in the view model from the view.

Now, I want to control the click event on the button and I would like to use a Command for only this event.

So my question is:

Is there any way to associate a command only for click event and keep the rest of events as above, through Drop and DragEnter events?

It seems like It can be done using Prism (see here) by importing below reference:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

But my problem here is that it is not compatible with WPF .NET 3.5 ... so any other way to do it without using Prism?

Willy
  • 9,848
  • 22
  • 141
  • 284

2 Answers2

2

Is there any way to associate a command only for click event and keep the rest of events as above, through Drop and DragEnter events?

Yes. This is exactly what the Command property of the Button is for. Bind this property to an ICommand property of your view model:

<Button Command="{Binding YourCommandProperty}" />

You will need an implementation of the ICommand interface. Prism provides one called DelegateCommand but you could define your own one. Please refer to the following blog post for more information: https://blog.magnusmontin.net/2013/06/30/handling-events-in-an-mvvm-wpf-application/. It includes a sample implementation.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Yes, right! By default command is bound to the click event ;) see here: https://stackoverflow.com/questions/40508556/what-events-trigger-a-command-in-a-button – Willy Jul 21 '17 at 10:02
1

Of course, you can use the Command property....

<Button AllowDrop="True"  
        Drop="btnDelete_Drop" 
        DragEnter="btnDelete_DragEnter"
        Command="{Binding ClickCommand}"
        Content="Delete"/>

and in your ViewModel implement an ICommand called ClickCommand

ajg
  • 1,743
  • 12
  • 14