I am trying to use the MVVM pattern for the first time. So I have an ItemsControl filled with my viewmodel objects, displayed using DataTemplates; the objects are "nodes" and "edges" represented in DataTemplates with Thumb and Polyline objects, and I want to be able to detect clicks and drags on the ItemsControl in order to move the nodes and edges.
Two questions:
- How do I attach mouse event handlers to the Polylines and Thumbs to be handled by the little viewmodels? (I could attach a Thumb.DragDelta handler to the ItemsControl, and e.OriginalSource points to the Thumb, but how do I get the corresponding viewmodel object?)
- How do I attach mouse event handlers to the ItemsControl to detect mouse clicks and drags on blank space? (answer is below)
Note: I know it might not be considered a proper ViewModel if it's directly handling events of the View. But the important point is, I need to handle mouse events and I am not sure how to attach them.