In a WPF application using MVVM, I have a usercontrol with a listview item. In run time, it will use databinding to fill the listview with a collection of objects.
What is the correct way to attach a double click event to the items in the listview so that when an item in the list view is doubleclicked, A corresponding event in the view model is fired and has a reference to the item clicked?
How can it be done in a clean MVVM way i.e. no code behind in the View?
Please, code behind is not a bad thing at all. Unfortunately, quite a lot people in the WPF community got this wrong.
MVVM is not a pattern to eliminate the code behind. It is to separate the view part (appearance, animations, etc.) from the logic part (workflow). Furthermore, you are able to unit test the logic part.
I know enough scenarios where you have to write code behind because data binding is not a solution to everything. In your scenario I would handle the DoubleClick event in the code behind file and delegate this call to the ViewModel.
Sample applications that use code behind and still fulfill the MVVM separation can be found here:
Win Application Framework (WAF) - https://github.com/jbe2277/waf
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With