0

How to create Command for Event Setter handler when we follow MVVM pattern in wpf. My xmal code is as:

<ListBox.ItemContainerStyle>
         <Style TargetType="ListBoxItem">
             <EventSetter Event="Drop" Handler="horizontalListBox2_Drop" />
         </Style>
    </ListBox.ItemContainerStyle>
naina
  • 63
  • 1
  • 9

1 Answers1

-2

You don't need a command to follow MVVM, you may just as well use that handler and code behind and invoke something on the VM. But you could create a markup extension that does this for you without the need for that. Here is an example where i create an extension to filter a collection that way. If you don't have complex parameters you of course can use the curly notation as well.

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • I am using MVVM Model in my application so i can not create Handle in the code behind. Please tell me this is how to possible in mvvm. – naina Jun 10 '16 at 11:43
  • @naina: I told you how you can do it without code behind, also you are wrong: As i said, **code behind is not a problem in MVVM**, that's just a common misconception. – H.B. Jun 10 '16 at 11:46
  • we need to perform some operation in this event and information is keep in view model so how to manage. – naina Jun 10 '16 at 11:53
  • @naina: The markup extension will invoke a command of the view-model, so all the information provided by the view-model is readily accessible to the command anyway. You can also design your extension to forward the event args as command parameter if those are relevant as well. – H.B. Jun 10 '16 at 12:22
  • I want like as by mvvm private void horizontalListBox_PreviewMouseLeftButtonDown3(object sender, MouseButtonEventArgs e) { if (sender is ListBoxItem) { ListBoxItem draggedItem = sender as ListBoxItem; DragDrop.DoDragDrop(draggedItem, draggedItem.DataContext, DragDropEffects.Move); draggedItem.IsSelected = true; } } – naina Jun 10 '16 at 12:28
  • @naina: I would just leave it at that if it works, creating an extension that does accomplish the same thing is unnecessary and complicated. – H.B. Jun 10 '16 at 12:41