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:
Polyline
's and Thumb
's 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?)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.
I found a way to handle events raised by objects in the DataTemplate.
(1) attach event handlers to the ItemsControl
<ItemsControl x:Name="_itemsControl"
Thumb.DragStarted="Node_DragStarted"
Thumb.DragDelta="Node_DragDelta"
Thumb.DragCompleted="Node_DragCompleted"
MouseDoubleClick="OnMouseDoubleClick"
.../>
(2) to find out which item the event applies to, treat the OriginalSource as a FrameworkElement, and get its DataContext:
void Node_DragStarted(object sender, DragStartedEventArgs e)
{
var os = (FrameworkElement)e.OriginalSource;
var vm = os.DataContext as ItemViewModel;
if (vm != null)
// do something with the item ViewModel
}
The ViewModel should be disconnected from the GUI, so it doesn't know anything about controls or mouseclicks.
Two options:
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