Trying to get the mouse Coordinate on clicked in UWP using MVVM

Manu Michael Samuel 131 Reputation points
2021-01-05T07:16:11.993+00:00

Hi all,
Am new in UWP trying to get the mouse coordinates while clicking on a image surface. Architecture using is MVVM. Can I get a working sample on this event ? or reference towards it.

Thank you.
Developer technologies | Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,866 Reputation points
    2021-01-05T08:39:54.277+00:00

    Hello @Manu Michael Samuel , Welcome to Microsoft Q&A

    Trying to get the mouse Coordinate on clicked in UWP

    For your scenario, you could detect Image control's Tapped event with x:bind way within MVVM architecture design. And get position relative to current page like the following.

    <Grid>  
        <Image Source="ms-appx:///Assets/test.PNG" Stretch="Fill" Tapped="{x:Bind Image_Tapped}" />  
    </Grid>  
    

    Code behind

    private void Image_Tapped(object sender, TappedRoutedEventArgs e)  
    {  
        var point = e.GetPosition(this);  
    }  
    

    Update

    public class ViewModel  
    {  
      
        public ViewModel()  
        {  
      
        }  
      
        public  void Image_Tapped(object sender, TappedRoutedEventArgs e)  
        {  
            var point = e.GetPosition(Window.Current.Content);  
        }  
    }  
    

    Usage

    <Page.DataContext>  
        <local:ViewModel x:Name="MyViewModel" />  
    </Page.DataContext>  
    <Grid>  
        <Image  
            Source="ms-appx:///Assets/test.PNG"  
            Stretch="Fill"  
            Tapped="{x:Bind MyViewModel.Image_Tapped}" />  
    </Grid>  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Xiaodi Yan 876 Reputation points MVP
    2021-01-05T08:52:40.257+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.