WPF: Capture Click Coordinates of an Image in an ItemsControl

It can be tricky to get the click coordinates of a control inside an ItemsControl. Here is how you do it:


<ItemsControl Name="FileImages">
            <StackPanel Orientation="Horizontal" />
            <Image Name="FileImage" Source="{Binding}" Focusable="True" MouseUp="FileImage_OnMouseUp" />

Code Behind:

private void FileImage_OnMouseUp(object sender, MouseButtonEventArgs e)
    var frameworkElement = (FrameworkElement)sender;
    var fileImage = (IInputElement)frameworkElement.FindName("FileImage");

    var point = e.GetPosition(fileImage);

        string.Format("X: {0}; Y: {1}", point.X, point.Y),
Tagged on: ,

Leave a Reply

Your email address will not be published. Required fields are marked *