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

I have included four photos in the 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 a "selected" function. my selected function looks like this

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

      

How can I assign the source of the selected image (sender) to the new Image object ?. something similar to the following

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

      

Is there a way to get the source dynamically?

+3


source to share


2 answers


Send the sender as an image and you can use the Source property:



private void selected(object sender, MouseButtonEventArgs e)
{
    Image newimage = new Image();
    newimage.Source = ((Image)sender).Source;
}

      

+5


source


Use the OriginalSource property of the event and pass it to Image:



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

      

+1


source







All Articles