MouseMove Event in WPF

Topics: Actions & Coroutines
Dec 23, 2011 at 11:59 AM


I want to use the MouseMove event in WPF from ViewModel.

I have used the following Xaml Code

<InkCanvas Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="myInkCanvas"  Background="LightYellow" Margin="6,0,12,61" cal:Message.Attach="OnMouseMove($eventArgs)" Strokes="{Binding CanvasStrokes,IsAsync=True}">

I have defined the OnMouseMove() method in VM

public void OnMouseMove(MouseEventArgs e)

The GetPosition method requres the relative InputElement as a parameter. In my case this is InkCanvas

How can I pass this element to the ViewModel ?

Any guidance will be a great help.


Dec 23, 2011 at 1:48 PM

In this case, I would recommend that you use code behind. Wire the event like normal, get the position, then call a method on your view model to add the point. Otherwise your view model is going to get tangled up with highly view specific concerns. You can get the ViewModel from the DataContext.