Guy Wald
Guy Wald

Reputation: 599

Display mouse position in Silverlight 5, MVVM

For the record: I'm new to Silverlight and XAML.

I'm trying to simply display the mouse coordinates relative in my canvas (or any other element). After reading a lot on the subject, unfortunantly I havn't figured this our yet. My Silverlight 5 project uses MVVM with Caliburn micro.

How can this be done? Would Appreciate help on implementing this.

Thanks, Guy.

Upvotes: 0

Views: 1127

Answers (3)

Venkatesh
Venkatesh

Reputation: 37

you have to catch the mouse click event and there are some event handlers you can use it to capture the position.

private void CadLayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
                double point1 = e.GetPosition(bitmapCadView.UIElement).X;
                double point2 = e.GetPosition(bitmapCadView.UIElement).Y;
    }

Upvotes: 0

Tonio
Tonio

Reputation: 743

To complete tnw Answer : You have to start mouse capture on object : MyElement.CaptureMouse()

In the handler method, you can use GetPosition on MouseEventArgs with an UIElement in parameter for relative coordinates...

public void Handle_MouseMove(object e, MouseEventArgs f)
{
     var point = f.GetPosition(MyElement);
}

Upvotes: 0

tnw
tnw

Reputation: 13887

You need to be a little more specific with what control you're trying to get this with, but have you tried wiring something up to the MouseMove event? Like MouseMove="Handle_MouseMove"

public void Handle_MouseMove(object sender, MouseEventArgs args) 
{
    mouseVerticalPosition = args.GetPosition(null).Y;
    mouseHorizontalPosition = args.GetPosition(null).X;
}

Is that what you're looking for?

If you want this to happen on the entire canvas, you can instead wire it up to PointerMoved

See MouseEventArgs. This can get you the X and Y position and it can be used with the MouseUp, MouseDown, and MouseMove events.

Upvotes: 3

Related Questions