Get the source of image when user clicks it in c#

Total Post:104

Points:728
C# 
C#
 1243  View(s)
Ratings:
Rate this:

I have included four photos in xaml code as follows

                <Image Grid.Column="0"
                       Source="Assets/1.png"
                       Name="m1"
                       MouseLeftButtonDown="selected"/>
                <Image Grid.Column="1"
                       Source="Assets/2.png"
                       Name="m2"
                       MouseLeftButtonDown="selected"/>
                <Image Grid.Column="2"
                       Source="Assets/3.png"
                       Name="m3"
                       MouseLeftButtonDown="selected"/>
                <Image Grid.Column="3"
                       Source="Assets/4.png"
                       Name="m4"
                       MouseLeftButtonDown="selected"/>

I want to get the source of the image in "selected" function. my selected function is as follows

private void selected(object sender, MouseButtonEventArgs e)
    {
        //do somethings....
    }

How can i assign the source of the selected image(sender) to a new Image object?. something similar to follows

Image newimage = new Image();
newimage.Source = //something..

Is there a way to dynamically get the source?

  1. Post:412

    Points:2888
    Re: Get the source of image when user clicks it in c#

    Hi Marcel, 

    Use OriginalSource property of event and cast it to Image:

    var clickedImage = (Image)e.OriginalSource;
    Image newimage = new Image();
    newimage.Source = clickedImage.Source;

      Modified On Apr-04-2018 05:45:11 AM

Answer