I am trying to use the MVVM pattern for the first time. So I have an ItemsControl
filled with my viewmodel objects, displayed using DataTemplate
's; the objects are "nodes" and "edges" represented in DataTemplate
's 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
Polyline
's andThumb
's to be handled by the little viewmodels? (I could attach aThumb.DragDelta
handler to theItemsControl
ande.OriginalSource
points to theThumb
, 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.