GET THE SOURCE OF IMAGE WHEN USER CLICKS IT IN C#

marcel ethan

Total Post:105

Points:735
Posted by  marcel ethan
C# 
C#
 1049  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. Pravesh Singh

    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;

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!